001: package tide.sources;
002:
003: import java.awt.image.BufferedImage;
004: import snow.utils.gui.GUIUtils;
005: import javax.imageio.ImageIO;
006: import snow.utils.StringUtils; //import snow.utils.gui.ImageUtils;
007: import snow.texteditor.SimpleEditor;
008: import javax.swing.event.*;
009: import java.util.*;
010: import snow.utils.storage.FileUtils;
011: import java.io.*;
012: import java.awt.EventQueue;
013: import tide.editor.MainEditorFrame;
014: import java.awt.event.*;
015: import snow.utils.DateUtils;
016: import java.awt.BorderLayout;
017: import snow.utils.gui.CloseControlPanel;
018: import javax.swing.*;
019: import java.util.List;
020: import snow.sortabletable.*;
021:
022: /** Browse all files that are not sources.
023: */
024: public final class ResourcesExplorer extends JDialog {
025: class RF {
026: final File f;
027: final String relName;
028:
029: public RF(File f, String relName) {
030: this .f = f;
031: this .relName = relName;
032: }
033: }
034:
035: final List<RF> allResourceFiles = new ArrayList<RF>();
036:
037: final private String[] columns = new String[] { "path",
038: "last-modified", "size [kB]" };
039: final private int[] COLUMN_PREFERED_SIZES = new int[] { 30, 7, 4 };
040: final private JTable table;
041: final private SortableTableModel stm;
042: final private FileTableModel ftm;
043: private final static String TITLE = "Project resources explorer";
044:
045: final private JTextArea codeToPasteArea = new JTextArea(6, 45);
046: final int baseL;
047:
048: final private File srcRoot, viewRoot, editedFile;
049:
050: public ResourcesExplorer(final File srcRoot, final File viewRoot,
051: final File editedFile) {
052: super (MainEditorFrame.instance, "Resources explorer from "
053: + viewRoot, false);
054:
055: this .srcRoot = srcRoot;
056: this .viewRoot = viewRoot;
057: this .editedFile = editedFile;
058:
059: baseL = FileUtils.getCanonicalName(srcRoot).length();
060: long totLength = 0;
061: for (File fi : FileUtils.getAllResourceFilesRecurse(viewRoot,
062: Arrays.asList(".java", ".scc", "thumbs.db"))) {
063: allResourceFiles.add(new RF(fi, fi.getAbsolutePath()
064: .substring(baseL).replace('\\', '/')));
065: totLength += fi.length();
066: }
067: setTitle(getTitle() + " [" + allResourceFiles.size()
068: + " files, " + FileUtils.formatSize(totLength) + "]");
069:
070: final JPanel southPanel = new JPanel(new BorderLayout());
071: add(southPanel, BorderLayout.SOUTH);
072: final CloseControlPanel ccp = new CloseControlPanel(this ,
073: false, true, "Close");
074: southPanel.add(ccp, BorderLayout.SOUTH);
075:
076: southPanel.add(new JScrollPane(codeToPasteArea),
077: BorderLayout.CENTER);
078: codeToPasteArea.setEditable(false);
079: codeToPasteArea.setBackground(getBackground());
080:
081: ftm = new FileTableModel();
082: stm = new SortableTableModel(ftm);
083: table = new JTable(stm);
084: stm.installGUI(table);
085:
086: add(new JScrollPane(table), BorderLayout.CENTER);
087:
088: final MultiSearchPanel msp = new MultiSearchPanel("Filter: ",
089: null, stm);
090: add(msp, BorderLayout.NORTH);
091:
092: EventQueue.invokeLater(new Runnable() {
093: public void run() {
094: msp.requestFocus();
095: }
096: });
097:
098: setSize(600, 600);
099: setLocation(200, 10);
100: setVisible(true);
101:
102: table.addMouseListener(new MouseAdapter() {
103: @Override
104: public void mousePressed(MouseEvent me) {
105: if (me.getClickCount() == 2) {
106: view();
107: }
108: }
109:
110: @Override
111: public void mouseReleased(MouseEvent me) {
112: }
113: });
114:
115: table.getSelectionModel().addListSelectionListener(
116: new ListSelectionListener() {
117: public void valueChanged(ListSelectionEvent lse) {
118: createCode();
119: }
120: });
121: }
122:
123: void createCode() {
124: codeToPasteArea.setText("");
125: StringBuilder sb = new StringBuilder();
126:
127: int posMod = stm.getIndexInUnsortedFromTablePos(table
128: .getSelectedRow());
129: if (posMod == -1)
130: return;
131:
132: RF selFile = allResourceFiles.get(posMod);
133: String rn = selFile.relName;
134:
135: String simpleJavaName = "SomeProjectClassName";
136: if (editedFile != null) {
137: simpleJavaName = editedFile.getName();
138: simpleJavaName = StringUtils.removeCharsAtEnd(
139: simpleJavaName, 5);
140: }
141:
142: sb
143: .append("Code to get the resource, works in IDE, jar and jnlp mode:");
144: // NOTE: ClassLoader.getSystem... don't work in the jnlp mode !!
145: sb.append("\n InputStream is = " + simpleJavaName
146: + ".class.getClassLoader().getResourceAsStream(\"" + rn
147: + "\");");
148: sb.append("\n if(is==null) { is = new FileInputStream(\""
149: + rn + "\"); }");
150: sb
151: .append("\n // usage example: BufferedImage bim = ImageIO.read(is);");
152:
153: if (this .editedFile != null) {
154: String par = FileUtils.getCanonicalName(this .editedFile
155: .getParentFile());
156: String sel = FileUtils.getCanonicalName(selFile.f);
157:
158: if (sel.startsWith(par)) {
159: // may help to know...
160: sb.append("\n // relative name from "
161: + simpleJavaName + ": "
162: + sel.substring(par.length()));
163: }
164: }
165:
166: codeToPasteArea.setText(sb.toString());
167: }
168:
169: void view() {
170: int posMod = stm.getIndexInUnsortedFromTablePos(table
171: .getSelectedRow());
172: if (posMod == -1)
173: return;
174:
175: RF selFile = allResourceFiles.get(posMod);
176:
177: final String ext = FileUtils.getExtension(selFile.f.getName())
178: .toLowerCase();
179:
180: // try as image, always.
181: try {
182: // Todo: thumb
183: BufferedImage bim = ImageIO.read(selFile.f);
184: // todo (2): use T-restorizer util...
185: GUIUtils.displayInDialog(this , selFile.relName,
186: new JScrollPane(new JLabel(new ImageIcon(bim))));
187: return; // ok
188: } catch (Exception ign) {
189: }
190:
191: // in all other cases: shows the text.
192:
193: SimpleEditor sed = new SimpleEditor("Resource "
194: + selFile.relName, false, false);
195: try {
196: sed.setText(selFile.f);
197: } catch (Exception e) {
198: e.printStackTrace();
199: }
200:
201: }
202:
203: class FileTableModel extends FineGrainTableModel {
204: public Object getValueAt(int row, int col) {
205: RF it = allResourceFiles.get(row);
206: if (col == 0) {
207: return it.relName;
208: }
209: if (col == 1) {
210: return DateUtils.formatDateAndTimeHuman(it.f
211: .lastModified());
212: }
213:
214: if (col == 2) {
215: return it.f.length() / 1000;
216: }
217:
218: return "?";
219: }
220:
221: @Override
222: public int getPreferredColumnWidth(int column) {
223: if (column >= 0 && column < COLUMN_PREFERED_SIZES.length)
224: return COLUMN_PREFERED_SIZES[column];
225: return -1;
226: }
227:
228: @Override
229: public int compareForColumnSort(int row1, int row2, int col) {
230: if (col == 1) {
231: // Slow
232: return Long.valueOf(
233: allResourceFiles.get(row1).f.lastModified())
234: .compareTo(
235: allResourceFiles.get(row2).f
236: .lastModified());
237: }
238:
239: if (col == 2) {
240: // Slow
241: return Long.valueOf(
242: allResourceFiles.get(row1).f.length())
243: .compareTo(
244: allResourceFiles.get(row2).f.length());
245: }
246:
247: // uses the getValueAt
248: return super .compareForColumnSort(row1, row2, col);
249: }
250:
251: @Override
252: public String getColumnName(int col) {
253: return columns[col];
254: }
255:
256: public int getRowCount() {
257: return allResourceFiles.size();
258: }
259:
260: public int getColumnCount() {
261: return columns.length;
262: }
263:
264: @Override
265: public boolean isCellEditable(int r, int c) {
266: return false;
267: }
268:
269: }
270:
271: }
|