1 /*
2  * Diese Datei ist Teil des GDialog Projektes:
3  * "Gigaset-Erweiterung f�r ProjectX"
4  *
5  * Das GDialog Projekt ist freigegeben unter
6  * der GNU Public Licence (GPL), deren Text sich in
7  * dem Quellen-Verzeichnis befindet. Ist er dort nicht
8  * mehr vorhanden, so kann er unter http://www.gnu.org/licenses/gpl.html
9  * eingesehen werden.
10  */
11 package de.m740.projectx.gigaset;
12 
13 import java.awt.event.MouseEvent;
14 import java.io.File;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.HashMap;
18 import java.util.List;
19 
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.event.ChangeEvent;
23 import javax.swing.event.ChangeListener;
24 import javax.swing.event.EventListenerList;
25 
26 /**
27  *
28  * @author arnaud
29  *
30  */
31 public abstract class GPanelAbstract extends JPanel implements GPanelTableIF {
32     // ----------------------------------------------------------
33     /**
34      * File comparator A to Z
35      */
36     class FileComparatorAZ implements Comparator {
37 
compare(Object arg0, Object arg1)38         public int compare(Object arg0, Object arg1) {
39             try {
40                 GAufnahmeIF tmpAufnahme = (GAufnahmeIF) arg0;
41                 String tmpFN0 = tmpAufnahme.getOriginalTitel();
42                 tmpAufnahme = (GAufnahmeIF) arg1;
43                 String tmpFN1 = tmpAufnahme.getOriginalTitel();
44                 return tmpFN0.compareToIgnoreCase(tmpFN1);
45             } catch (Exception e) {
46                 // ignore
47             }
48             return 0;
49         }
50 
51     }
52 
53     // ----------------------------------------------------------
54     /**
55      * File comparator Z to A
56      */
57     class FileComparatorZA implements Comparator {
58 
compare(Object arg0, Object arg1)59         public int compare(Object arg0, Object arg1) {
60             try {
61                 return -(getFileComparatorAZ().compare(arg0, arg1));
62             } catch (Exception e) {
63                 // ignore
64             }
65             return 0;
66         }
67 
68     }
69 
70     // ----------------------------------------------------------
71     /**
72      * File comparator newer to older
73      */
74     class FileComparatorON implements Comparator {
75 
compare(Object arg0, Object arg1)76         public int compare(Object arg0, Object arg1) {
77             try {
78                 GAufnahmeIF tmpAufnahme = (GAufnahmeIF) arg0;
79                 String tmpFN0 = tmpAufnahme.getCridFilename();
80                 tmpAufnahme = (GAufnahmeIF) arg1;
81                 String tmpFN1 = tmpAufnahme.getCridFilename();
82                 return tmpFN0.compareTo(tmpFN1);
83             } catch (Exception e) {
84                 // ignore
85             }
86             return 0;
87         }
88 
89     }
90 
91     // ----------------------------------------------------------
92     /**
93      * File comparator newer to older
94      */
95     class FileComparatorNO implements Comparator {
96 
compare(Object arg0, Object arg1)97         public int compare(Object arg0, Object arg1) {
98             try {
99                 return -(getFileComparatorON().compare(arg0, arg1));
100             } catch (Exception e) {
101                 // ignore
102             }
103             return 0;
104         }
105 
106     }
107 
108     // ----------------------------------------------------------
109     protected List aAufnahmen = null;
110 
111     protected GTableModel aTableModel = new GTableModel();
112 
113     protected GTable aTableAufnahmen = new GTable(aTableModel);
114 
115     protected JScrollPane spAufnahmen = new JScrollPane(aTableAufnahmen);
116 
117     private FileComparatorAZ aFileComparatorAZ = null;
118 
119     private FileComparatorZA aFileComparatorZA = null;
120 
121     private FileComparatorNO aFileComparatorNO = null;
122 
123     private FileComparatorON aFileComparatorON = null;
124 
125     protected int aSortSelector = SORT_FILES_NO;
126 
127     private GDialog aDialog = null;
128 
GPanelAbstract()129     public GPanelAbstract() {
130         super();
131         initialize();
132     }
133 
134     /**
135      *
136      */
initialize()137     private void initialize() {
138         try {
139             aTableAufnahmen.setDefaultRenderer(Object.class,
140                     new GTableCellRenderer());
141             // Font font = new Font("Monospaced", Font.PLAIN, 12);
142             // aTableAufnahmen.setFont(font);
143         } catch (RuntimeException exc) {
144             GConsole.setErrorMessage(exc);
145         }
146     }
147 
148     /**
149      * @param inpIndex
150      * @param inpAufnahme
151      * @param inpHash
152      */
addAufnahme(int inpIndex, GAufnahmeIF inpAufnahme, HashMap inpHash)153     protected void addAufnahme(int inpIndex, GAufnahmeIF inpAufnahme,
154             HashMap inpHash) {
155         // doppelte Titel pr�fen ...
156         String tmpTitel = inpAufnahme.getOriginalTitel();
157         Object tmpObj = inpHash.get(tmpTitel);
158         if (tmpObj instanceof GAufnahmeContainer) {
159             GAufnahmeContainer tmpAufnahmeContainer = (GAufnahmeContainer) tmpObj;
160             GAufnahmeIF tmpAufnahmeIF = tmpAufnahmeContainer.getAufnahme();
161             tmpAufnahmeIF.setSerie(true);
162             inpAufnahme.setSerie(true);
163             tmpAufnahmeContainer.addChild(inpAufnahme);
164         } else {
165             // ... und Doubletten nummerieren
166             GAufnahmeContainer tmpAufnahmeContainer = new GAufnahmeContainer(
167                     inpAufnahme);
168             inpHash.put(tmpTitel, tmpAufnahmeContainer);
169             // push to table
170             int row = aTableModel.getRowCount();
171             aTableModel.addRow(tmpAufnahmeContainer);
172             aTableModel.fireTableRowsInserted(row, row);
173         }
174     }
175 
176     /**
177      *
178      * number children from old to newer
179      */
countChildren(int selector)180     public void countChildren(int selector) {
181         int anza = (aTableModel == null) ? 0 : aTableModel.getRowCount();
182         for (int i = 0; i < anza; i++) {
183             Object tmpObject = aTableModel.getObjectAtRow(i);
184             if (tmpObject instanceof GAufnahmeContainer) {
185                 ((GAufnahmeContainer) tmpObject).countChildren(selector);
186             }
187         } // for i
188     }
189 
190     /**
191      *
192      *
193      */
sortiereAufnahmen(Comparator inpComparator)194     private void sortiereAufnahmen(Comparator inpComparator) {
195         clearSelection();
196         aTableModel.clear();
197         aTableModel.fireTableDataChanged();
198         fireStateChanged();
199         //
200         int anza = (aAufnahmen == null) ? 0 : aAufnahmen.size();
201         if (anza > 1) {
202             Collections.sort(aAufnahmen, inpComparator);
203         }
204         HashMap tmpHash = new HashMap(anza);
205         for (int i = 0; i < anza; i++) {
206             try {
207                 GAufnahmeIF tmpAufnahme = (GAufnahmeIF) aAufnahmen.get(i);
208                 addAufnahme(i, tmpAufnahme, tmpHash);
209             } catch (Exception exc) {
210                 GConsole.setErrorMessage(exc);
211             }
212         } // for i
213         // evtl letzte Tree-�nderungen sichtbar machen
214         aTableModel.fireTableRowsUpdated(0, aTableModel.getRowCount() - 1);
215         tmpHash = null;
216 
217         if (aTableModel.getRowCount() == 0) {
218             // falls keine Aufnahmen vorh.
219             getDialog().zeigeInfos(-1); // soll auch nix angezeigt w.!
220         }
221     }
222 
223     /**
224      *
225      *
226      */
sortiereAufnahmen(int selector)227     public void sortiereAufnahmen(int selector) {
228         try {
229             switch (selector) {
230             case SORT_FILES_AZ:
231                 sortiereAufnahmen(getFileComparatorAZ());
232                 break;
233             case SORT_FILES_ZA:
234                 sortiereAufnahmen(getFileComparatorZA());
235                 break;
236             case SORT_FILES_NO:
237                 sortiereAufnahmen(getFileComparatorNO());
238                 break;
239             case SORT_FILES_ON:
240                 sortiereAufnahmen(getFileComparatorON());
241                 break;
242             }
243         } catch (RuntimeException exc) {
244             GConsole.setErrorMessage(exc);
245         }
246     }
247 
getSelectedAufnahme()248     public GAufnahmeIF getSelectedAufnahme() {
249         try {
250             if (aTableAufnahmen == null || aTableModel == null) {
251                 return null;
252             }
253             int nr = aTableAufnahmen.getSelectedRow();
254             if (nr < 0) {
255                 return null;
256             }
257             return aTableModel.getAufnahmeAtRow(nr);
258         } catch (Exception exc) {
259             GConsole.setErrorMessage(exc);
260         }
261         return null; // error
262     }
263 
264     /**
265      *
266      */
readSelectedAufnahme()267     public int readSelectedAufnahme() {
268         try {
269             int nr = aTableAufnahmen.getSelectedRow();
270             if (nr < 0) {
271                 return -1;
272             }
273             GAufnahmeIF tmpAufnahme = aTableModel.getAufnahmeAtRow(nr);
274             tmpAufnahme.readAufnahme();
275             return nr;
276         } catch (Exception e) {
277             GConsole.setErrorMessage(e);
278         }
279         return -1; // error
280     }
281 
getSelectedExtension()282     public String getSelectedExtension() {
283         GAufnahmeIF tmpAufnahme = getSelectedAufnahme();
284         if (tmpAufnahme == null) {
285             return null;
286         }
287         return tmpAufnahme.getExtension();
288     }
289 
getSelectedOutname()290     public String getSelectedOutname() {
291         String tmpString = getSelectedTitel();
292         // wenn ein Punkt im Titel ist,
293         // dann noch die Extension anh�ngen,
294         // weil sonst PjX den letzten Teil abschneidet
295         if (tmpString != null && tmpString.indexOf('.') >= 0) {
296             String tmpString2 = getSelectedExtension();
297             if (tmpString2 != null) {
298                 tmpString += tmpString2;
299             }
300         }
301         return tmpString;
302     }
303 
getSelectedTitel()304     public String getSelectedTitel() {
305         GAufnahmeIF tmpAufnahme = getSelectedAufnahme();
306         if (tmpAufnahme == null) {
307             return null;
308         }
309         return tmpAufnahme.getTitel();
310     }
311 
shortPathOf(String inpPath)312     protected String shortPathOf(String inpPath) {
313         if (inpPath == null) {
314             return "";
315         }
316         char tmpC = File.separatorChar;
317         int x1 = inpPath.indexOf(tmpC);
318         String tmpString = "";
319         if (x1 > 0) {
320             int len = inpPath.length();
321             tmpString += inpPath.substring(0, x1 + 1);
322             inpPath = (len - 1 == x1) ? "" : inpPath.substring(x1 + 1);
323         }
324         int x2 = inpPath.lastIndexOf(File.separatorChar);
325         tmpString += (x2 == x1 || x2 < 0) ? inpPath : "..."
326                 + inpPath.substring(x2);
327         return tmpString;
328     }
329 
330     /**
331      * Adds a <code>ChangeListener</code> to this tabbedpane.
332      *
333      * @param l
334      *            the <code>ChangeListener</code> to add
335      * @see #fireStateChanged
336      * @see #removeChangeListener
337      */
addChangeListener(ChangeListener l)338     public void addChangeListener(ChangeListener l) {
339         removeChangeListener(l); // prevent double listening!
340         listenerList.add(ChangeListener.class, l);
341     }
342 
343     /**
344      * Removes a <code>ChangeListener</code> from this tabbedpane.
345      *
346      * @param l
347      *            the <code>ChangeListener</code> to remove
348      * @see #fireStateChanged
349      * @see #addChangeListener
350      */
removeChangeListener(ChangeListener l)351     public void removeChangeListener(ChangeListener l) {
352         listenerList.remove(ChangeListener.class, l);
353     }
354 
355     /**
356      * Sends a <code>ChangeEvent</code>, whose source is this tabbedpane, to
357      * each listener. This method method is called each time a
358      * <code>ChangeEvent</code> is received from the model.
359      *
360      * @see #addChangeListener
361      * @see EventListenerList
362      */
fireStateChanged()363     protected void fireStateChanged() {
364         // Guaranteed to return a non-null array
365         Object[] listeners = listenerList.getListenerList();
366         // Process the listeners last to first, notifying
367         // those that are interested in this event
368         ChangeEvent tmpChangeEvent = null;
369         for (int i = listeners.length - 2; i >= 0; i -= 2) {
370             if (listeners[i] == ChangeListener.class) {
371                 // Lazily create the event:
372                 if (tmpChangeEvent == null)
373                     tmpChangeEvent = new ChangeEvent(this);
374                 ((ChangeListener) listeners[i + 1])
375                         .stateChanged(tmpChangeEvent);
376             }
377         }
378     }
379 
getDialog()380     protected GDialog getDialog() {
381         return aDialog;
382     }
383 
setDialog(GDialog inpDialog)384     protected void setDialog(GDialog inpDialog) {
385         aDialog = inpDialog;
386     }
387 
clearSelection()388     protected void clearSelection() {
389         aTableAufnahmen.getSelectionModel().clearSelection();
390     }
391 
392     /**
393      *
394      */
actionDone(boolean inpOK)395     private void actionDone(boolean inpOK) {
396         if (!inpOK) {
397             clearSelection();
398         }
399         getDialog().actionDone(inpOK, true);
400     }
401 
isExpander()402     private boolean isExpander() {
403         boolean ok = false;
404         try {
405             if (aTableAufnahmen == null || aTableModel == null) {
406                 return ok;
407             }
408             int nr = aTableAufnahmen.getSelectedRow();
409             if (nr < 0) {
410                 return ok;
411             }
412             Object tmpObject = aTableModel.getObjectAtRow(nr);
413             if (tmpObject instanceof GAufnahmeContainer) {
414                 GAufnahmeContainer tmpContainer = (GAufnahmeContainer) tmpObject;
415                 if (!tmpContainer.hasChildren()) {
416                     return false;
417                 }
418                 ok = true;
419                 if (tmpContainer.isExpanded()) {
420                     // collapse node
421                     List tmpList = tmpContainer.getDoubles();
422                     int anz = (tmpList == null) ? 0 : tmpList.size();
423                     int i;
424                     for (i = 0; i < anz; i++) {
425                         GAufnahmeContainer tmpDouble = (GAufnahmeContainer) tmpList
426                                 .get(0);
427                         aTableModel.removeRow(tmpDouble);
428                         tmpContainer.removeDouble(tmpDouble);
429                     } // for i
430                     aTableModel.fireTableRowsDeleted(nr + 1, nr + i);
431                 } else {
432                     // expand node
433                     List tmpList = tmpContainer.getChildren();
434                     int anz = (tmpList == null) ? 0 : tmpList.size();
435                     int r = nr;
436                     for (int i = 0; i < anz; i++) {
437                         GAufnahmeIF tmpAufnahme = (GAufnahmeIF) tmpList.get(i);
438                         GAufnahmeContainer tmpDouble = new GAufnahmeContainer(
439                                 tmpAufnahme);
440                         aTableModel.addRow(++r, tmpDouble);
441                         tmpContainer.addDouble(tmpDouble);
442                     } // for i
443                     aTableModel.fireTableRowsInserted(nr + 1, r);
444                 }
445             }
446         } catch (Exception exc) {
447             GConsole.setErrorMessage(exc);
448         }
449         return ok;
450     }
451 
mouseClick(MouseEvent event)452     protected void mouseClick(MouseEvent event) {
453         int clicks = event.getClickCount();
454         int x = event.getX();
455         if (event.getButton() == 1 && clicks == 2 && x > 22) {
456             actionDone(true);
457         } else if (clicks == 1 & x < 21) {
458             if (isExpander()) {
459                 return;
460             }
461         }
462     }
463 
464     /**
465      * @return comparatorAZ
466      */
getFileComparatorAZ()467     protected FileComparatorAZ getFileComparatorAZ() {
468         if (aFileComparatorAZ == null) {
469             aFileComparatorAZ = new FileComparatorAZ();
470         }
471         return aFileComparatorAZ;
472     }
473 
474     /**
475      * @return fileComparatorZA
476      */
getFileComparatorZA()477     private FileComparatorZA getFileComparatorZA() {
478         if (aFileComparatorZA == null) {
479             aFileComparatorZA = new FileComparatorZA();
480         }
481         return aFileComparatorZA;
482     }
483 
484     /**
485      * @return fileComparatorNO
486      */
getFileComparatorNO()487     private FileComparatorNO getFileComparatorNO() {
488         if (aFileComparatorNO == null) {
489             aFileComparatorNO = new FileComparatorNO();
490         }
491         return aFileComparatorNO;
492     }
493 
494     /**
495      * @return fileComparatorON
496      */
getFileComparatorON()497     protected FileComparatorON getFileComparatorON() {
498         if (aFileComparatorON == null) {
499             aFileComparatorON = new FileComparatorON();
500         }
501         return aFileComparatorON;
502     }
503 
504     /**
505      * @return sortSelector
506      */
getSortSelector()507     protected int getSortSelector() {
508         return aSortSelector;
509     }
510 
511     /**
512      * @param inpSortSelector Festzulegender sortSelector
513      */
setSortSelector(int inpSortSelector)514     protected void setSortSelector(int inpSortSelector) {
515         aSortSelector = inpSortSelector;
516     }
517 }
518