View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.file.action;
14  
15  import java.awt.Component;
16  import java.awt.Dimension;
17  import java.io.File;
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.swing.AbstractListModel;
22  import javax.swing.Box;
23  import javax.swing.BoxLayout;
24  import javax.swing.Icon;
25  import javax.swing.JLabel;
26  import javax.swing.JList;
27  import javax.swing.JOptionPane;
28  import javax.swing.JScrollPane;
29  import javax.swing.ListSelectionModel;
30  import javax.swing.border.EmptyBorder;
31  
32  import org.abstracthorizon.aequo.action.BaseAction;
33  import org.abstracthorizon.aequo.file.FileCompareEntry;
34  import org.abstracthorizon.aequo.file.FilesModel;
35  
36  /**
37   * Base action for operations on file model
38   *
39   * @author Daniel Sendula
40   */
41  public abstract class FileBaseAction extends BaseAction {
42      
43      /** File model */
44      protected FilesModel filesModel;
45      
46      /** Preselected entries. Entries are going to be collected for dialog and stored here to avoid repeating the operation */
47      protected FileCompareEntry[] preselectedEntries;
48      
49      /**
50       * Constructor
51       */
52      public FileBaseAction() {
53      }
54  
55      /**
56       * Sets file model
57       * @param model file model
58       */
59      public void setFilesModel(FilesModel model) {
60          this.filesModel = model;
61      }
62      
63      /**
64       * Returns file model
65       * @return file model
66       */
67      public FilesModel getFilesModel() {
68          return filesModel;
69      }
70      
71      /**
72       * Returns if exactly one row is selected
73       * @return <code>true</code> if exactly one row is selected
74       */
75      public boolean isOneSelected() {
76          ListSelectionModel selModel = getFilesModel().getSelectionModel();
77          int min = selModel.getMinSelectionIndex();
78          int max = selModel.getMaxSelectionIndex();
79          return ((min >= 0) && (min == max));
80      }
81  
82      /**
83       * Returns if selection is valid
84       * @return <code>true</code> if selection is valid
85       */
86      public boolean isSelectionValid() {
87          ListSelectionModel selModel = getFilesModel().getSelectionModel();
88          int min = selModel.getMinSelectionIndex();
89          int max = selModel.getMaxSelectionIndex();
90          return ((min >= 0) && (min <= max));
91      }
92      
93      /**
94       * Helper method that collects all selected entries
95       * @param model model
96       * @return array of all selected entries
97       */
98      protected static FileCompareEntry[] collectSelectedEntries(FilesModel model) {
99          ArrayList<FileCompareEntry> entries = new ArrayList<FileCompareEntry>();
100         ListSelectionModel selModel = model.getSelectionModel();
101         int min = selModel.getMinSelectionIndex();
102         int max = selModel.getMaxSelectionIndex();
103         int i = min;
104         int level = 0;
105         while (i <= max) {
106             if (selModel.isSelectedIndex(i)) {
107                 FileCompareEntry entry = model.get(i);
108                 if (level >= entry.getLevel()) {
109                     FileCompareEntry previousEntry = model.get(i - 1);
110                     collectRecursively(previousEntry, entries);
111                 }
112                 entries.add(entry);
113                 level = entry.getLevel();
114             }
115             i++;
116         }
117         collectRecursively(entries.get(entries.size() - 1), entries);
118 
119         FileCompareEntry[] res = new FileCompareEntry[entries.size()];
120         return entries.toArray(res);
121     }
122 
123     /**
124      * Collects entries recursively
125      * @param entry starting entry
126      * @param list list to collect entries to
127      */
128     protected static void collectRecursively(FileCompareEntry entry, List<FileCompareEntry> list) {
129         if (entry.hasChildren()) {
130             for (FileCompareEntry e : entry.getChildren()) {
131                 list.add(e);
132                 collectRecursively(e, list);
133             }
134         }
135     }
136 
137     /**
138      * This method shows confirmation dialog
139      * @return result as in {@link JOptionPane#showConfirmDialog(java.awt.Component, Object)}
140      */
141     protected int showConfirmDialog() {
142         int res = super.showConfirmDialog();
143         if (res != JOptionPane.OK_OPTION) {
144             preselectedEntries = null;
145         }
146         return res;
147     }
148     
149     /**
150      * Creates a panel with message and a list of files operation to be performed on.
151      * @return created panel
152      */
153     public Object updateMessage() {
154         preselectedEntries = collectSelectedEntries(getFilesModel());
155         
156         Box box = new Box(BoxLayout.Y_AXIS);
157         Component comp = null;
158         if (message instanceof String) {
159             JLabel l = new JLabel((String)message);
160             l.setBorder(new EmptyBorder(0, 0, 10, 0));
161             l.setAlignmentX(Component.LEFT_ALIGNMENT);
162             comp = l;
163         } else if (message instanceof Icon) {
164             JLabel l = new JLabel((Icon)message);
165             l.setAlignmentX(Component.LEFT_ALIGNMENT);
166             l.setBorder(new EmptyBorder(0, 0, 10, 0));
167             comp = l;
168         } else if (message instanceof Component) {
169             comp = (Component)message;
170         }
171         if (comp != null) {
172             box.add(comp);
173         }
174         
175         int c = column;
176         if (c < 0) {
177             c = 0;
178         }
179         final int len = getFilesModel().getData(c).getAbsolutePath().length();
180         
181         JList list = new JList(new AbstractListModel() {
182             
183             public Object getElementAt(int index) {
184                 int c = column;
185                 if (c < 0) {
186                     c = 0;
187                 }
188                 File f = preselectedEntries[index].getData(c);
189                 String name = f.getAbsolutePath();
190                 if (name.length() > len) {
191                     name = name.substring(len);
192                 } else {
193                     name = "/";
194                 }
195                 return name;
196             }
197 
198             public int getSize() {
199                 return preselectedEntries.length;
200             }
201             
202         });
203         list.setVisibleRowCount(5);
204         JScrollPane sp = new JScrollPane(list);
205         sp.setAlignmentX(Component.LEFT_ALIGNMENT);
206         box.add(sp);
207         Dimension d = box.getPreferredSize();
208         if (d.width >  450) {
209             box.setPreferredSize(new Dimension(450, d.height));
210         }
211         
212         return box;
213     }
214     
215     /**
216      * It returns preselected entries if they exist. If not it just returns
217      * result of {@link #collectSelectedEntries(FilesModel)}.
218      * If preselected entries did exist it removes them.
219      * 
220      * @return preselected entries
221      */
222     protected FileCompareEntry[] getPreselectedEntries() {
223         if (preselectedEntries != null) {
224             FileCompareEntry[] res = preselectedEntries;
225             preselectedEntries = null;
226             return res;
227         }
228         return collectSelectedEntries(getFilesModel());
229     }
230 }