svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / gui / projectpanel / ProjectDocumentsPanelPage.java @ 46301
History | View | Annotate | Download (20 KB)
1 |
package org.gvsig.app.project.documents.gui.projectpanel; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import org.gvsig.app.project.documents.gui.*; |
6 |
import java.awt.Dimension; |
7 |
import java.awt.FlowLayout; |
8 |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.ActionListener; |
10 |
import java.awt.event.ComponentAdapter; |
11 |
import java.awt.event.ComponentEvent; |
12 |
import java.awt.event.MouseAdapter; |
13 |
import java.awt.event.MouseEvent; |
14 |
import java.beans.PropertyChangeEvent; |
15 |
import java.beans.PropertyChangeListener; |
16 |
import java.io.File; |
17 |
import java.util.Collections; |
18 |
import java.util.Comparator; |
19 |
import java.util.Iterator; |
20 |
import java.util.List; |
21 |
import javax.swing.ButtonGroup; |
22 |
import javax.swing.DefaultListCellRenderer; |
23 |
import javax.swing.JComponent; |
24 |
import javax.swing.JLabel; |
25 |
import javax.swing.JList; |
26 |
import javax.swing.JOptionPane; |
27 |
import javax.swing.JPanel; |
28 |
import javax.swing.JRadioButton; |
29 |
import javax.swing.JScrollPane; |
30 |
import javax.swing.ScrollPaneConstants; |
31 |
import javax.swing.SwingUtilities; |
32 |
import javax.swing.event.ListSelectionEvent; |
33 |
import javax.swing.event.ListSelectionListener; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.gvsig.andami.IconThemeHelper; |
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
38 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
39 |
import org.gvsig.app.ApplicationLocator; |
40 |
import org.gvsig.app.ApplicationManager; |
41 |
import org.gvsig.app.project.Project; |
42 |
import org.gvsig.app.project.ProjectManager; |
43 |
import org.gvsig.app.project.documents.Document; |
44 |
import org.gvsig.app.project.documents.DocumentManager; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.i18n.I18nManager; |
47 |
import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
48 |
import org.gvsig.tools.observer.Observable; |
49 |
import org.gvsig.tools.observer.Observer; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
51 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
52 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
53 |
import org.gvsig.tools.util.Invocable; |
54 |
import org.gvsig.utils.DefaultListModel; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
public class ProjectDocumentsPanelPage |
59 |
extends ProjectDocumentsPanelPageView
|
60 |
implements ProjectPanelPage {
|
61 |
|
62 |
private final Logger LOG = LoggerFactory.getLogger(ProjectGeneralPropertiesPage.class); |
63 |
|
64 |
private Project project;
|
65 |
private DocumentManager currentDocumentType;
|
66 |
private final PropertyChangeListener projectPropertyChangeListener; |
67 |
private final Observer projectObserver; |
68 |
|
69 |
public ProjectDocumentsPanelPage() {
|
70 |
this.project = null; |
71 |
this.projectPropertyChangeListener = new PropertyChangeListener() { |
72 |
@Override
|
73 |
public void propertyChange(PropertyChangeEvent evt) { |
74 |
updateProject(); |
75 |
} |
76 |
}; |
77 |
this.projectObserver = new Observer() { |
78 |
@Override
|
79 |
public void update(Observable observable, Object notification) { |
80 |
updateProject(); |
81 |
} |
82 |
}; |
83 |
this.initComponents();
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public void setData(Object data) { |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public String getTitle() { |
92 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
93 |
return i18n.getTranslation("_Documents"); |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public int getPriority() { |
98 |
return 1000; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public boolean whenAccept() { |
103 |
return true; |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public boolean whenApply() { |
108 |
return true; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public boolean whenCancel() { |
113 |
return true; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public JComponent asJComponent() { |
118 |
return this; |
119 |
} |
120 |
|
121 |
private void initComponents() { |
122 |
this.initDocumentTypes();
|
123 |
this.btnDocumentNew.addActionListener(new ActionListener() { |
124 |
@Override
|
125 |
public void actionPerformed(ActionEvent e) { |
126 |
doDocumentNew(); |
127 |
} |
128 |
}); |
129 |
this.btnDocumentOpen.addActionListener(new ActionListener() { |
130 |
@Override
|
131 |
public void actionPerformed(ActionEvent e) { |
132 |
doDocumentOpen(); |
133 |
} |
134 |
}); |
135 |
this.btnDocumentRename.addActionListener(new ActionListener() { |
136 |
@Override
|
137 |
public void actionPerformed(ActionEvent e) { |
138 |
doDocumentRename(); |
139 |
} |
140 |
}); |
141 |
this.btnDocumentDelete.addActionListener(new ActionListener() { |
142 |
@Override
|
143 |
public void actionPerformed(ActionEvent e) { |
144 |
doDocumentDelete(); |
145 |
} |
146 |
}); |
147 |
this.btnDocumentProperties.addActionListener(new ActionListener() { |
148 |
@Override
|
149 |
public void actionPerformed(ActionEvent e) { |
150 |
doDocumentProperties(); |
151 |
} |
152 |
}); |
153 |
this.btnProjectProperties.addActionListener(new ActionListener() { |
154 |
@Override
|
155 |
public void actionPerformed(ActionEvent e) { |
156 |
doProjectProperties(); |
157 |
} |
158 |
}); |
159 |
this.lstDocuments.addListSelectionListener(new ListSelectionListener() { |
160 |
@Override
|
161 |
public void valueChanged(ListSelectionEvent e) { |
162 |
if (e.getValueIsAdjusting()) {
|
163 |
return;
|
164 |
} |
165 |
doDocumentsSelectionChanged(); |
166 |
} |
167 |
}); |
168 |
this.lstDocuments.addMouseListener(new MouseAdapter() { |
169 |
@Override
|
170 |
public void mouseClicked(MouseEvent e) { |
171 |
if( e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2 ) { |
172 |
doDocumentOpen(); |
173 |
} |
174 |
} |
175 |
}); |
176 |
|
177 |
this.lstDocuments.setCellRenderer(new DefaultListCellRenderer(){ |
178 |
@Override
|
179 |
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
180 |
JLabel c = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
181 |
c.setIcon(null);
|
182 |
if(value instanceof Document){ |
183 |
Document doc = (Document) value; |
184 |
if(((Document) value).isTemporary()){ |
185 |
c.setIcon(IconThemeHelper.getImageIcon("temporary-table-icon"));
|
186 |
} |
187 |
} |
188 |
return c;
|
189 |
} |
190 |
}); |
191 |
this.translate();
|
192 |
} |
193 |
|
194 |
private void translate() { |
195 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
196 |
manager.translate(this.lblCreationDate);
|
197 |
manager.translate(this.lblSavedIn);
|
198 |
manager.translate(this.lblSessionName);
|
199 |
manager.translate(this.lblDocument);
|
200 |
manager.translate(this.lblDocumentTypes);
|
201 |
manager.translate(this.lblSessionProperties);
|
202 |
manager.translate(this.lblCreationDate);
|
203 |
manager.translate(this.lblCreationDate);
|
204 |
manager.translate(this.lblCreationDate);
|
205 |
manager.translate(this.lblCreationDate);
|
206 |
manager.translate(this.lblCreationDate);
|
207 |
manager.translate(this.lblCreationDate);
|
208 |
|
209 |
manager.translate(this.btnDocumentDelete);
|
210 |
manager.translate(this.btnDocumentNew);
|
211 |
manager.translate(this.btnDocumentOpen);
|
212 |
manager.translate(this.btnDocumentProperties);
|
213 |
manager.translate(this.btnDocumentRename);
|
214 |
manager.translate(this.btnProjectProperties);
|
215 |
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void setProject(Project project) { |
220 |
if( this.project!=null ) { |
221 |
this.project.removePropertyChangeListener(this.projectPropertyChangeListener); |
222 |
this.project.deleteObserver(this.projectObserver); |
223 |
} |
224 |
this.project = project;
|
225 |
if(project!=null){ |
226 |
project.addPropertyChangeListener(this.projectPropertyChangeListener);
|
227 |
project.addObserver(this.projectObserver);
|
228 |
} |
229 |
updateProject(); |
230 |
} |
231 |
|
232 |
private void updateProject() { |
233 |
if( !SwingUtilities.isEventDispatchThread() ) { |
234 |
SwingUtilities.invokeLater(new Runnable() { |
235 |
@Override
|
236 |
public void run() { |
237 |
updateProject(); |
238 |
} |
239 |
}); |
240 |
return;
|
241 |
} |
242 |
updateProjectDocumens(); |
243 |
updateProjectProperties(); |
244 |
} |
245 |
|
246 |
private void updateProjectProperties() { |
247 |
if (project != null) { |
248 |
this.txtSessionName.setText(project.getName());
|
249 |
File path = this.project.getFile(); |
250 |
if (path != null) { |
251 |
this.txtSavedIn.setText(path.toString());
|
252 |
} else {
|
253 |
this.txtSavedIn.setText(""); |
254 |
} |
255 |
this.txtCreationDate.setText(project.getCreationDate());
|
256 |
} |
257 |
} |
258 |
|
259 |
private void initDocumentTypes() { |
260 |
SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
261 |
|
262 |
this.documentTypesContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); |
263 |
|
264 |
List<DocumentManager> factories = ProjectManager.getInstance().getDocumentManagers();
|
265 |
Collections.sort(factories, new Comparator<DocumentManager>() { |
266 |
|
267 |
@Override
|
268 |
public int compare(DocumentManager arg0, DocumentManager arg1) { |
269 |
return arg0.getPriority() - arg1.getPriority();
|
270 |
} |
271 |
|
272 |
}); |
273 |
this.currentDocumentType = factories.get(0); |
274 |
JRadioButton selectButton = null; |
275 |
ButtonGroup group = new ButtonGroup(); |
276 |
JPanel panel = new JPanel(); |
277 |
for (DocumentManager documentFactory : factories) {
|
278 |
if (!identityManager.getCurrentIdentity().isAuthorized(
|
279 |
Document.ACCESS_DOCUMENT_AUTHORIZATION,
|
280 |
documentFactory, |
281 |
documentFactory.getTypeName())) { |
282 |
continue;
|
283 |
} |
284 |
JRadioButton button = this.createDocumentTypeButton(documentFactory); |
285 |
group.add(button); |
286 |
panel.add(button); |
287 |
if( documentFactory == this.currentDocumentType ) { |
288 |
selectButton = button; |
289 |
} |
290 |
} |
291 |
|
292 |
JScrollPane scroll = new JScrollPane(panel); |
293 |
Dimension dim = panel.getPreferredSize();
|
294 |
scroll.setMinimumSize(new Dimension(dim.width+2, dim.height + 35)); |
295 |
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
|
296 |
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
297 |
|
298 |
this.documentTypesContainer.setLayout(new BorderLayout()); |
299 |
this.documentTypesContainer.add(scroll, BorderLayout.CENTER); |
300 |
selectButton.setSelected(true);
|
301 |
} |
302 |
|
303 |
private JRadioButton createDocumentTypeButton(final DocumentManager documentFactory) { |
304 |
final JRadioButton rb = new JRadioButton(); |
305 |
|
306 |
rb.setIcon(documentFactory.getIcon()); |
307 |
rb.setSelectedIcon(documentFactory.getIconSelected()); |
308 |
rb.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); |
309 |
rb.setText(documentFactory.getTitle()); |
310 |
rb.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); |
311 |
rb.setName(documentFactory.getTypeName()); |
312 |
rb.addChangeListener(new javax.swing.event.ChangeListener() {
|
313 |
@Override
|
314 |
public void stateChanged(javax.swing.event.ChangeEvent e) { |
315 |
if (rb.isSelected()) {
|
316 |
currentDocumentType = documentFactory; |
317 |
updateProjectDocumens(); |
318 |
} |
319 |
} |
320 |
}); |
321 |
return rb;
|
322 |
} |
323 |
|
324 |
private void openDocumentWindow(Document doc) { |
325 |
if (doc == null) { |
326 |
return;
|
327 |
} |
328 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
329 |
ApplicationManager application = ApplicationLocator.getManager(); |
330 |
IWindow window = doc.getFactory().getMainWindow(doc); |
331 |
if (window == null) { |
332 |
application.message( |
333 |
i18n.getTranslation("error_opening_the_document"),
|
334 |
JOptionPane.WARNING_MESSAGE
|
335 |
); |
336 |
return;
|
337 |
} |
338 |
application.getUIManager().addWindow( |
339 |
window, |
340 |
MDIManager.ALIGN_FIRST_LINE_END_CASCADE |
341 |
); |
342 |
project.setModified(true);
|
343 |
} |
344 |
|
345 |
private void updateProjectDocumens() { |
346 |
if (project == null) { |
347 |
return;
|
348 |
} |
349 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
350 |
this.lblDocument.setText(i18n.getTranslation("documentos_existentes")); |
351 |
|
352 |
String documentsTypeName = this.currentDocumentType.getTypeName(); |
353 |
List<Document> documents = project.getDocuments(documentsTypeName); |
354 |
DefaultListModel model = new DefaultListModel(documents); |
355 |
this.lstDocuments.setModel(model);
|
356 |
this.lblDocument.setText(currentDocumentType.getTitle());
|
357 |
updateDocumentButtons(); |
358 |
} |
359 |
|
360 |
private void updateDocumentButtons() { |
361 |
this.btnDocumentNew.setEnabled(true); |
362 |
if (this.lstDocuments.getSelectedIndex() != -1) { |
363 |
this.btnDocumentOpen.setEnabled(true); |
364 |
this.btnDocumentDelete.setEnabled(true); |
365 |
this.btnDocumentRename.setEnabled(true); |
366 |
this.btnDocumentProperties.setEnabled(true); |
367 |
} else {
|
368 |
btnDocumentOpen.setEnabled(false);
|
369 |
btnDocumentDelete.setEnabled(false);
|
370 |
btnDocumentRename.setEnabled(false);
|
371 |
btnDocumentProperties.setEnabled(false);
|
372 |
} |
373 |
} |
374 |
|
375 |
private void doDocumentNew() { |
376 |
String documentsTypeName = this.currentDocumentType.getTypeName(); |
377 |
ProjectManager projectManager = ProjectManager.getInstance(); |
378 |
Iterator<? extends Document> documents = projectManager.createDocumentsByUser(documentsTypeName, new Invocable() { |
379 |
@Override
|
380 |
public Object call(Object... args) { |
381 |
Iterator<? extends Document> docs = (Iterator<? extends Document>) args[0]; |
382 |
addNewDocuments(docs); |
383 |
return null; |
384 |
} |
385 |
}); |
386 |
addNewDocuments(documents); |
387 |
} |
388 |
|
389 |
private void addNewDocuments(Iterator<? extends Document> documents) { |
390 |
while ( documents != null && documents.hasNext() ) { |
391 |
try {
|
392 |
Document document = documents.next();
|
393 |
project.addDocument(document); |
394 |
openDocumentWindow(document); |
395 |
} catch (Exception e) { |
396 |
LOG.warn("Can't open document", e);
|
397 |
} |
398 |
} |
399 |
this.lstDocuments.requestFocus();
|
400 |
this.lstDocuments.setSelectedIndex(this.lstDocuments.getModel().getSize()-1); |
401 |
} |
402 |
|
403 |
private void doDocumentOpen() { |
404 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
405 |
ApplicationManager application = ApplicationLocator.getManager(); |
406 |
|
407 |
int[] indexes = this.lstDocuments.getSelectedIndices(); |
408 |
for (int i = indexes.length - 1; i >= 0; i--) { |
409 |
int index = indexes[i];
|
410 |
if (index == -1) { |
411 |
return;
|
412 |
} |
413 |
String documentsTypeName = this.currentDocumentType.getTypeName(); |
414 |
List<Document> documents = project.getDocuments(documentsTypeName); |
415 |
Document doc = documents.get(index);
|
416 |
if (!doc.isAvailable()) {
|
417 |
application.messageDialog( |
418 |
i18n.getTranslation("document_not_available"),
|
419 |
i18n.getTranslation("abrir"),
|
420 |
JOptionPane.WARNING_MESSAGE
|
421 |
); |
422 |
} |
423 |
IWindow win = doc.getMainWindow(); |
424 |
openDocumentWindow(doc); |
425 |
} |
426 |
|
427 |
} |
428 |
|
429 |
private void doDocumentRename() { |
430 |
ApplicationManager application = ApplicationLocator.getManager(); |
431 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
432 |
|
433 |
int index = this.lstDocuments.getSelectedIndex(); |
434 |
if (index == -1) { |
435 |
return;
|
436 |
} |
437 |
String DoumentTypeName = this.currentDocumentType.getTypeName(); |
438 |
List<Document> documents = project.getDocuments(DoumentTypeName); |
439 |
Document doc = documents.get(index);
|
440 |
String name = application.inputDialog(
|
441 |
i18n.getTranslation("introduce_nombre"),
|
442 |
i18n.getTranslation("renombrar"),
|
443 |
JOptionPane.QUESTION_MESSAGE,
|
444 |
doc.getName() |
445 |
); |
446 |
if(name != null) { |
447 |
for (Document document : documents) { |
448 |
if (StringUtils.equals(document.getName(), name)) {
|
449 |
application.messageDialog( |
450 |
i18n.getTranslation("elemento_ya_existe"),
|
451 |
i18n.getTranslation("warning"),
|
452 |
JOptionPane.WARNING_MESSAGE
|
453 |
); |
454 |
return;
|
455 |
} |
456 |
} |
457 |
doc.setName(name); |
458 |
updateProjectDocumens(); |
459 |
this.project.setModified(true); |
460 |
} |
461 |
} |
462 |
|
463 |
private void doDocumentDelete() { |
464 |
ApplicationManager application = ApplicationLocator.getManager(); |
465 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
466 |
|
467 |
int res = application.confirmDialog(
|
468 |
i18n.getTranslation("confirmar_borrar"),
|
469 |
i18n.getTranslation("borrar"),
|
470 |
JOptionPane.YES_NO_OPTION,
|
471 |
JOptionPane.INFORMATION_MESSAGE,
|
472 |
"Confirm remove document from project"
|
473 |
); |
474 |
int lastremoved = 0; |
475 |
if (res == JOptionPane.YES_OPTION) { |
476 |
String DoumentTypeName = this.currentDocumentType.getTypeName(); |
477 |
int[] indexes = this.lstDocuments.getSelectedIndices(); |
478 |
for (int i = indexes.length - 1; i >= 0; i--) { |
479 |
int index = indexes[i];
|
480 |
List<Document> documents = project.getDocuments(DoumentTypeName); |
481 |
Document doc = documents.get(index);
|
482 |
if (doc.isLocked()) {
|
483 |
application.messageDialog( |
484 |
i18n.getTranslation("locked_element_it_cannot_be_deleted"),
|
485 |
i18n.getTranslation("Information"),
|
486 |
JOptionPane.INFORMATION_MESSAGE
|
487 |
); |
488 |
return;
|
489 |
} |
490 |
PluginServices.getMDIManager().closeSingletonWindow(doc); |
491 |
project.removeDocument(doc); |
492 |
lastremoved = index; |
493 |
} |
494 |
if (lastremoved >= this.lstDocuments.getModel().getSize()) { |
495 |
this.lstDocuments.setSelectedIndex(this.lstDocuments.getModel().getSize() - 1); |
496 |
} else if (lastremoved >= 0) { |
497 |
this.lstDocuments.setSelectedIndex(lastremoved);
|
498 |
} |
499 |
project.setModified(true);
|
500 |
} |
501 |
|
502 |
} |
503 |
|
504 |
private void doDocumentProperties() { |
505 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
506 |
ApplicationManager application = ApplicationLocator.getManager(); |
507 |
|
508 |
int index = this.lstDocuments.getSelectedIndex(); |
509 |
if (index == -1) { |
510 |
return;
|
511 |
} |
512 |
String DoumentTypeName = this.currentDocumentType.getTypeName(); |
513 |
List<Document> documents = project.getDocuments(DoumentTypeName); |
514 |
Document doc = documents.get(index);
|
515 |
if (!doc.isAvailable()) {
|
516 |
application.messageDialog( |
517 |
i18nManager.getTranslation("document_not_available"),
|
518 |
i18nManager.getTranslation("properties"),
|
519 |
JOptionPane.WARNING_MESSAGE
|
520 |
); |
521 |
return;
|
522 |
} |
523 |
IWindow properties = doc.getFactory().getPropertiesWindow(doc); |
524 |
application.getUIManager().addWindow( |
525 |
properties |
526 |
); |
527 |
this.lstDocuments.setSelectedIndex(index);
|
528 |
project.setModified(true);
|
529 |
} |
530 |
|
531 |
private void doProjectProperties() { |
532 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
533 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
534 |
ProjectProperties panel = new ProjectProperties(project);
|
535 |
panel.addComponentListener(new ComponentAdapter() { |
536 |
@Override
|
537 |
public void componentHidden(ComponentEvent e) { |
538 |
updateProjectProperties(); |
539 |
} |
540 |
}); |
541 |
winmgr.showWindow( |
542 |
panel, |
543 |
i18nManager.getTranslation("project_properties"),
|
544 |
WindowManager.MODE.WINDOW |
545 |
); |
546 |
} |
547 |
|
548 |
private void doDocumentsSelectionChanged() { |
549 |
this.updateDocumentButtons();
|
550 |
} |
551 |
} |