1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2006, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.exec;
  28 
  29 import com.sun.javatest.TemplateUtilities;
  30 import java.io.File;
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.LinkedHashMap;
  34 import java.util.Map;
  35 import javax.swing.event.TableModelEvent;
  36 import javax.swing.table.AbstractTableModel;
  37 
  38 public class FileSystemTableModel extends AbstractTableModel  {
  39 
  40     // Names of the columns.
  41     // TODO - i18!
  42     static protected String[]  cNames = {"File Name", "Name", "Description"};
  43 
  44     // Types of the columns.
  45     static protected Class[]  cTypes = { String.class,
  46                                          String.class, String.class};
  47 
  48     private FileTableFilter filter = null;
  49 
  50     private FileTableNode root;
  51     private ArrayList<FileTableNode> data;
  52     private LinkedHashMap<String, String[]> fileData;
  53     private boolean allowTraversDirs;
  54     private File defTemplateDir;
  55 
  56     public FileSystemTableModel(String file, FileTableFilter flt, File defTemplateDir, boolean allowTraversDirs) {
  57         setFilter(flt);
  58         this.allowTraversDirs = allowTraversDirs;
  59         this.defTemplateDir = defTemplateDir;
  60         init(file);
  61     }
  62 
  63     public void fireTableDataChanged() {
  64     }
  65 
  66     public void resetTable(String file, FileTableFilter flt) {
  67         setFilter(flt);
  68         init(file);
  69         fireTableChanged(new TableModelEvent(this));
  70     }
  71 
  72     public void resetTable(String file) {
  73         init(file);
  74         fireTableChanged(new TableModelEvent(this));
  75     }
  76 
  77     public int getColumnCount() {
  78         return cNames.length;
  79     }
  80 
  81     public String getColumnName(int column) {
  82         return cNames[column];
  83     }
  84 
  85     public Class getColumnClass(int column) {
  86         return cTypes[column];
  87     }
  88 
  89     private void setFilter(FileTableFilter flt) {
  90         filter = flt;
  91     }
  92 
  93     private void init(String f) {
  94         File file = new File(f);
  95         init(file);
  96     }
  97 
  98     public File getCurrentDir() {
  99         return root.getFile();
 100     }
 101 
 102     private void init(final File file) {
 103         root = new FileTableNode(file, 'r');
 104         data = new ArrayList<>();
 105 
 106 //        if(allowTraversDirs) {
 107 //            data.add(new FileTableNode(root.getFile(), 'u'));
 108 //        }
 109         if (allowTraversDirs || !file.equals(defTemplateDir)) {
 110             if (file.getParent() != null) {
 111                 data.add(new FileTableNode(file.getParentFile(), 'u'));
 112             }
 113         }
 114 
 115         File [] lst = file.listFiles();
 116         if (lst != null)
 117             Arrays.sort(lst);
 118 
 119         if (lst != null && lst.length > 0) {
 120 //            if (allowTraversDirs) {
 121                 for (int i = 0; i < lst.length; i++) {
 122                     if (lst[i].isDirectory()) {
 123                         data.add(new FileTableNode(lst[i], 'd'));
 124                     }
 125                 }   // for
 126 //            }
 127             for (int i = 0; i < lst.length; i++) {
 128                 if (!lst[i].isDirectory()) {
 129                     if (filter == null || filter.isApplicableFile(lst[i])) {
 130                         data.add(new FileTableNode(lst[i], 'f'));
 131                     }
 132                 }
 133             }   // for
 134         }
 135     }
 136 
 137     public Object getValueAt(int rowIndex, int columnIndex) {
 138         if (rowIndex < 0 || rowIndex >= data.size() ) return null;
 139         FileTableNode f = data.get(rowIndex);
 140         File file = f.getFile();
 141         if (file == null) return null;
 142         if (columnIndex == 0) return f;//file.getName();
 143         if (file.isDirectory()) return "";
 144         if (columnIndex == 1) return getConfigName(file);
 145         if (columnIndex == 2) return getConfigDesc(file);
 146         return null;
 147     }
 148 
 149     public File getNode(int rowIndex) {
 150         if (rowIndex < 0 || rowIndex >= data.size() ) return null;
 151         return (data.get(rowIndex)).getFile();
 152     }
 153 
 154 
 155     public int getRowCount() {
 156         return data.size();
 157     }
 158 
 159     private String getConfigName(File file) {
 160         return getInfo(file)[0];
 161     }
 162 
 163     private String getConfigDesc(File file) {
 164         return getInfo(file)[1];
 165     }
 166 
 167     private String[] getInfo(File file) {
 168 
 169         if (fileData == null) fileData = new LinkedHashMap<String, String[]>() {
 170                 protected boolean removeEldestEntry(Map.Entry<String, String[]> eldest) {
 171                     return size() > 500;
 172                 }
 173         };
 174         String key = file.getAbsolutePath();
 175         String[] value = fileData.get(key);
 176         if (value != null) {
 177             return value;
 178         }
 179 
 180         // refresh
 181         try {
 182             String[] data = new String[] {"", ""};
 183             /*
 184             String path = TemplateUtilities.getTemplateFromWd(file);
 185             if (path != null) {
 186                 TemplateUtilities.ConfigInfo ci = TemplateUtilities.getConfigInfo(new File(path));
 187                 if (ci != null) {
 188                     data = new String[] {ci.getName(), ci.getDescription()};
 189                 }
 190 
 191             }
 192             */
 193             TemplateUtilities.ConfigInfo ci = TemplateUtilities.getConfigInfo(file);
 194             if (ci != null) {
 195                 data = new String[] {ci.getName(), ci.getDescription()};
 196             }
 197             fileData.put(key, data);
 198             return data;
 199         }
 200         catch  (Exception e) {
 201             return new String[] {"", ""};
 202         }
 203     }
 204 
 205     static class FileTableFilter {
 206         FileTableFilter(String ext) {
 207             extension = ext;
 208         }
 209 
 210         protected boolean isApplicableFile(File f) {
 211             if (extension == null) return true;
 212             if (f.isDirectory()) return true;
 213             return f.getName().endsWith(extension);
 214         }
 215 
 216         private String extension;
 217     }
 218 
 219 }
 220 
 221 
 222 class FileTableNode {
 223 
 224     File file;
 225 
 226     private char mode;
 227 
 228     public FileTableNode(File file, char mode) {
 229         this.file = file;
 230         this.mode = mode;
 231     }
 232 
 233     public String toString() {
 234         return file.getName();
 235     }
 236 
 237     public File getFile() {
 238         return file;
 239     }
 240 
 241     public char getMode() {
 242         return mode;
 243     }
 244 
 245 }