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