KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > merge > builtin > visualizer > MergeDialogComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.merge.builtin.visualizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyVetoException JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.ref.Reference JavaDoc;
32 import java.lang.ref.WeakReference JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import javax.swing.JPopupMenu JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40 import javax.swing.plaf.TabbedPaneUI JavaDoc;
41 import org.openide.DialogDisplayer;
42 import org.openide.ErrorManager;
43
44 import org.openide.NotifyDescriptor;
45 import org.openide.actions.FileSystemAction;
46 import org.openide.awt.MouseUtils;
47 import org.openide.awt.JPopupMenuPlus;
48 import org.openide.cookies.CloseCookie;
49 import org.openide.cookies.SaveCookie;
50 import org.openide.nodes.Node;
51 import org.openide.windows.TopComponent;
52 import org.openide.windows.Workspace;
53 import org.openide.util.*;
54 import org.openide.util.actions.CallableSystemAction;
55 import org.openide.util.actions.SystemAction;
56 import org.openide.windows.WindowManager;
57 import org.netbeans.api.javahelp.Help;
58
59 /**
60  * This is a component, that acts as a non modal dialog.
61  * There are problems with accessibility to non-modal dialogs,
62  * therefore this approach was chosen.
63  * @author Martin Entlicher
64  */

65 public class MergeDialogComponent extends TopComponent implements ChangeListener JavaDoc {
66     
67     public static final String JavaDoc PROP_PANEL_CLOSING = "panelClosing"; // NOI18N
68
public static final String JavaDoc PROP_ALL_CLOSED = "allPanelsClosed"; // NOI18N
69
public static final String JavaDoc PROP_ALL_CANCELLED = "allPanelsCancelled"; // NOI18N
70
public static final String JavaDoc PROP_PANEL_SAVE = "panelSave"; // NOI18N
71

72     private Map JavaDoc<MergePanel, MergeNode> nodesForPanels = new HashMap JavaDoc<MergePanel, MergeNode>();
73     
74     /** Creates new form MergeDialogComponent */
75     public MergeDialogComponent() {
76         initComponents();
77         initListeners();
78         putClientProperty("PersistenceType", "Never");
79         setName(org.openide.util.NbBundle.getMessage(MergeDialogComponent.class, "MergeDialogComponent.title"));
80         getAccessibleContext().setAccessibleName(NbBundle.getMessage(MergeDialogComponent.class, "ACSN_Merge_Dialog_Component")); // NOI18N
81
getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MergeDialogComponent.class, "ACSD_Merge_Dialog_Component")); // NOI18N
82
mergeTabbedPane.getAccessibleContext().setAccessibleName(NbBundle.getMessage(MergeDialogComponent.class, "ACSN_Merge_Tabbed_Pane")); // NOI18N
83
mergeTabbedPane.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MergeDialogComponent.class, "ACSD_Merge_Tabbed_Pane")); // NOI18N
84
}
85     
86     public HelpCtx getHelpCtx() {
87         return new HelpCtx(MergeDialogComponent.class);
88     }
89     
90     /** This method is called from within the constructor to
91      * initialize the form.
92      * WARNING: Do NOT modify this code. The content of this method is
93      * always regenerated by the Form Editor.
94      */

95     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
96
private void initComponents() {
97         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
98
99         mergeTabbedPane = new javax.swing.JTabbedPane JavaDoc();
100         buttonsPanel = new javax.swing.JPanel JavaDoc();
101         okButton = new javax.swing.JButton JavaDoc();
102         cancelButton = new javax.swing.JButton JavaDoc();
103         helpButton = new javax.swing.JButton JavaDoc();
104
105         FormListener formListener = new FormListener();
106
107         setLayout(new java.awt.GridBagLayout JavaDoc());
108
109         mergeTabbedPane.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
110         mergeTabbedPane.setPreferredSize(new java.awt.Dimension JavaDoc(600, 600));
111         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
112         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
113         gridBagConstraints.weightx = 1.0;
114         gridBagConstraints.weighty = 1.0;
115         add(mergeTabbedPane, gridBagConstraints);
116
117         buttonsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
118
119         org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(MergeDialogComponent.class, "BTN_OK")); // NOI18N
120
okButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergeDialogComponent.class).getString("ACS_BTN_OKA11yDesc")); // NOI18N
121
okButton.addActionListener(formListener);
122         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
123         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
124         gridBagConstraints.weightx = 1.0;
125         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
126         buttonsPanel.add(okButton, gridBagConstraints);
127
128         org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(MergeDialogComponent.class, "BTN_Cancel")); // NOI18N
129
cancelButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergeDialogComponent.class).getString("ACS_BTN_CancelA11yDesc")); // NOI18N
130
cancelButton.addActionListener(formListener);
131         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
133         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
134         buttonsPanel.add(cancelButton, gridBagConstraints);
135
136         org.openide.awt.Mnemonics.setLocalizedText(helpButton, org.openide.util.NbBundle.getMessage(MergeDialogComponent.class, "BTN_Help")); // NOI18N
137
helpButton.setToolTipText(org.openide.util.NbBundle.getBundle(MergeDialogComponent.class).getString("ACS_BTN_HelpA11yDesc")); // NOI18N
138
helpButton.addActionListener(formListener);
139         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
140         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
141         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
142         buttonsPanel.add(helpButton, gridBagConstraints);
143
144         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.gridy = 1;
146         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
147         gridBagConstraints.weightx = 1.0;
148         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 11, 11);
149         add(buttonsPanel, gridBagConstraints);
150     }
151
152     // Code for dispatching events from components to event handlers.
153

154     private class FormListener implements java.awt.event.ActionListener JavaDoc {
155         FormListener() {}
156         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
157             if (evt.getSource() == okButton) {
158                 MergeDialogComponent.this.okButtonActionPerformed(evt);
159             }
160             else if (evt.getSource() == cancelButton) {
161                 MergeDialogComponent.this.cancelButtonActionPerformed(evt);
162             }
163             else if (evt.getSource() == helpButton) {
164                 MergeDialogComponent.this.helpButtonActionPerformed(evt);
165             }
166         }
167     }// </editor-fold>//GEN-END:initComponents
168

169     private void okButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_okButtonActionPerformed
170
// Add your handling code here:
171
//List panelsToCloseList;
172
Component JavaDoc[] panels;
173         synchronized (this) {
174             panels = mergeTabbedPane.getComponents();
175         }
176         boolean warning = false;
177         ArrayList JavaDoc<String JavaDoc> unsavedPanelNames = new ArrayList JavaDoc<String JavaDoc>();
178         ArrayList JavaDoc<SaveCookie> saveCookies = new ArrayList JavaDoc<SaveCookie>();
179         for (int i = 0; i < panels.length; i++) {
180             MergePanel panel = (MergePanel) panels[i];
181             if((panel.getNumUnresolvedConflicts() > 0) && (!warning))
182                 warning = true;
183             MergeNode node = nodesForPanels.get(panel);
184             SaveCookie sc;
185             if ((sc = node.getCookie(SaveCookie.class)) != null) {
186                 unsavedPanelNames.add(panel.getName());
187                 saveCookies.add(sc);
188             }
189         }
190         Object JavaDoc ret;
191         // XXX can format with one format string
192
if (unsavedPanelNames.size() == 1) {
193             ret = DialogDisplayer.getDefault().notify(
194             new NotifyDescriptor.Confirmation((warning)?NbBundle.getMessage(MergeDialogComponent.class,"SaveFileWarningQuestion",unsavedPanelNames.get(0)):
195                                               NbBundle.getMessage(MergeDialogComponent.class,"SaveFileQuestion",unsavedPanelNames.get(0)),
196                                               NotifyDescriptor.YES_NO_CANCEL_OPTION));
197         } else if (unsavedPanelNames.size() > 1) {
198             ret = DialogDisplayer.getDefault().notify(
199                 new NotifyDescriptor.Confirmation((warning)?NbBundle.getMessage(MergeDialogComponent.class,"SaveFilesWarningQuestion",new Integer JavaDoc(unsavedPanelNames.size())):
200                                                   NbBundle.getMessage(MergeDialogComponent.class,"SaveFilesQuestion",new Integer JavaDoc(unsavedPanelNames.size())),
201                                                   NotifyDescriptor.YES_NO_CANCEL_OPTION));
202         } else {
203             if(warning){
204                 ret = DialogDisplayer.getDefault().notify(
205                 new NotifyDescriptor.Confirmation(NbBundle.getMessage(MergeDialogComponent.class,"WarningQuestion",new Integer JavaDoc(unsavedPanelNames.size())),
206                                                   NotifyDescriptor.OK_CANCEL_OPTION));
207                 if(ret.equals(NotifyDescriptor.NO_OPTION))
208                     return;
209             }else
210                 ret = NotifyDescriptor.YES_OPTION;
211         }
212         if (!NotifyDescriptor.YES_OPTION.equals(ret) && !NotifyDescriptor.NO_OPTION.equals(ret)) return ;
213         if (NotifyDescriptor.YES_OPTION.equals(ret) || NotifyDescriptor.OK_OPTION.equals(ret)) {
214             for (SaveCookie sc: saveCookies) {
215                 IOException JavaDoc ioException = null;
216                 try {
217                     sc.save();
218                 } catch (UserQuestionException uqex) {
219                     Object JavaDoc status = DialogDisplayer.getDefault().notify(
220                         new NotifyDescriptor.Confirmation(uqex.getLocalizedMessage()));
221                     if (status == NotifyDescriptor.OK_OPTION || status == NotifyDescriptor.YES_OPTION) {
222                         boolean success;
223                         try {
224                             uqex.confirmed();
225                             success = true;
226                         } catch (IOException JavaDoc ioex) {
227                             success = false;
228                             ioException = ioex;
229                         }
230                         if (success) {
231                             try {
232                                 sc.save();
233                             } catch (IOException JavaDoc ioex) {
234                                 ioException = ioex;
235                             }
236                         }
237                     } else if (status != NotifyDescriptor.NO_OPTION) {
238                         // cancel
239
return ;
240                     }
241                 } catch (IOException JavaDoc ioEx) {
242                     ioException = ioEx;
243                 }
244                 if (ioException != null) {
245                     ErrorManager.getDefault().notify(ioException);
246                     // cancel the close - there was an error on save
247
return ;
248                 }
249             }
250         }
251         for (int i = 0; i < panels.length; i++) {
252             MergePanel panel = (MergePanel) panels[i];
253             try {
254                 fireVetoableChange(PROP_PANEL_CLOSING, null, panel);
255             } catch (PropertyVetoException JavaDoc pvex) {
256                 return ;
257             }
258             removeMergePanel(panel);
259         }
260     }//GEN-LAST:event_okButtonActionPerformed
261

262     private void helpButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_helpButtonActionPerformed
263
Help help = (Help) Lookup.getDefault().lookup(Help.class);
264         help.showHelp(getHelpCtx());
265     }//GEN-LAST:event_helpButtonActionPerformed
266

267     private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_cancelButtonActionPerformed
268
// Add your handling code here:
269
synchronized (this) {
270             try {
271                 fireVetoableChange(PROP_ALL_CANCELLED, null, null);
272             } catch (PropertyVetoException JavaDoc pvex) {}
273             close();
274         }
275     }//GEN-LAST:event_cancelButtonActionPerformed
276

277     protected void componentClosed() {
278         Component JavaDoc[] panels;
279         synchronized (this) {
280             try {
281                 fireVetoableChange(PROP_ALL_CANCELLED, null, null);
282             } catch (PropertyVetoException JavaDoc pvex) {}
283
284             panels = mergeTabbedPane.getComponents();
285         }
286         for (int i = 0; i < panels.length; i++) {
287             MergePanel panel = (MergePanel) panels[i];
288             removeMergePanel(panel);
289         }
290     }
291     
292     /** @return Preferred size of editor top component */
293     public Dimension JavaDoc getPreferredSize() {
294         Rectangle JavaDoc bounds = WindowManager.getDefault().getCurrentWorkspace().getBounds();
295         return new Dimension JavaDoc(bounds.width / 2, (int) (bounds.height / 1.25));
296     }
297
298     public int getPersistenceType() {
299         return TopComponent.PERSISTENCE_NEVER;
300     }
301
302     // Variables declaration - do not modify//GEN-BEGIN:variables
303
private javax.swing.JPanel JavaDoc buttonsPanel;
304     private javax.swing.JButton JavaDoc cancelButton;
305     private javax.swing.JButton JavaDoc helpButton;
306     private javax.swing.JTabbedPane JavaDoc mergeTabbedPane;
307     private javax.swing.JButton JavaDoc okButton;
308     // End of variables declaration//GEN-END:variables
309

310     public void addNotify() {
311         super.addNotify();
312                 javax.swing.JRootPane JavaDoc root = getRootPane();
313                 if (root != null) root.setDefaultButton(okButton);
314                 /*
315         javax.swing.SwingUtilities.invokeLater(new Runnable() {
316             public void run() {
317                 javax.swing.JRootPane root = getRootPane();
318                 if (root != null) root.setDefaultButton(okButton);
319             }
320         });
321                  */

322     }
323     
324     private void initListeners() {
325         mergeTabbedPane.addMouseListener(new PopupMenuImpl());
326         mergeTabbedPane.addChangeListener(this);
327     }
328
329     public void open(Workspace workspace) {
330         super.open(workspace);
331         requestActive();
332     }
333     
334     public synchronized void addMergePanel(MergePanel panel) {
335         mergeTabbedPane.addTab(panel.getName(), panel);
336         MergeNode node = new MergeNode(panel);
337         nodesForPanels.put(panel, node);
338         mergeTabbedPane.setSelectedComponent(panel);
339         setActivatedNodes(new Node[] { node });
340     }
341     
342     public synchronized void removeMergePanel(MergePanel panel) {
343         mergeTabbedPane.remove(panel);
344         nodesForPanels.remove(panel);
345         if (mergeTabbedPane.getTabCount() == 0) {
346             try {
347                 fireVetoableChange(PROP_ALL_CLOSED, null, null);
348             } catch (PropertyVetoException JavaDoc pvex) {
349                 return ;
350             }
351             close();
352         }
353     }
354     
355     public MergePanel getSelectedMergePanel() {
356         Component JavaDoc selected = mergeTabbedPane.getSelectedComponent();
357         if (selected == null || !(selected instanceof MergePanel)) return null;
358         return ((MergePanel) selected);
359     }
360     
361     private static JPopupMenu JavaDoc createPopupMenu(MergePanel panel) {
362         JPopupMenu JavaDoc popup = new JPopupMenuPlus();
363         SystemAction[] actions = panel.getSystemActions();
364         for (int i = 0; i < actions.length; i++) {
365             if (actions[i] == null) {
366                 popup.addSeparator();
367             } else if (actions[i] instanceof CallableSystemAction) {
368                 popup.add(((CallableSystemAction)actions[i]).getPopupPresenter());
369                 //add FileSystemAction to pop-up menu
370
} else if (actions[i] instanceof FileSystemAction) {
371                 popup.add(((FileSystemAction)actions[i]).getPopupPresenter());
372             }
373         }
374         return popup;
375     }
376     
377     /** Shows given popup on given coordinations and takes care about the
378      * situation when menu can exceed screen limits.
379      * Copied from org.netbeans.core.windows.frames.DefaultContainerImpl
380      */

381     private static void showPopupMenu(JPopupMenu JavaDoc popup, Point JavaDoc p, Component JavaDoc comp) {
382         SwingUtilities.convertPointToScreen(p, comp);
383         Dimension JavaDoc popupSize = popup.getPreferredSize();
384         Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
385         
386         if (p.x + popupSize.width > screenSize.width) {
387             p.x = screenSize.width - popupSize.width;
388         }
389         if (p.y + popupSize.height > screenSize.height) {
390             p.y = screenSize.height - popupSize.height;
391         }
392         SwingUtilities.convertPointFromScreen(p, comp);
393         popup.show(comp, p.x, p.y);
394     }
395     
396     /** Listen on tabbed pane merge panel selection */
397     public void stateChanged(javax.swing.event.ChangeEvent JavaDoc changeEvent) {
398         MergePanel panel = (MergePanel) mergeTabbedPane.getSelectedComponent();
399         if (panel != null) {
400             Node node = nodesForPanels.get(panel);
401             if (node != null) {
402                 setActivatedNodes(new Node[] { node });
403             }
404         }
405     }
406     
407     /** Popup menu reaction implementation */
408     private class PopupMenuImpl extends MouseUtils.PopupMouseAdapter {
409         public PopupMenuImpl () {}
410         
411         /** Called when the seqeunce of mouse events should lead to actual
412          * showing of the popup menu. */

413         protected void showPopup(java.awt.event.MouseEvent JavaDoc mouseEvent) {
414             TabbedPaneUI JavaDoc tabUI = mergeTabbedPane.getUI();
415             int clickTab = tabUI.tabForCoordinate(mergeTabbedPane, mouseEvent.getX(), mouseEvent.getY());
416             MergePanel panel = getSelectedMergePanel();
417             if (panel == null) {
418                 return;
419             }
420             if (clickTab != -1) {
421                 //Click is on valid tab, not on empty area in tab
422
showPopupMenu(createPopupMenu(panel), mouseEvent.getPoint(), mergeTabbedPane);
423             }
424         }
425         
426     }
427     
428     private class MergeNode extends org.openide.nodes.AbstractNode implements PropertyChangeListener JavaDoc, SaveCookie {
429         
430         private Reference JavaDoc<MergePanel> mergePanelRef;
431         
432         public MergeNode(MergePanel panel) {
433             super(org.openide.nodes.Children.LEAF);
434             panel.addPropertyChangeListener(WeakListeners.propertyChange(this, panel));
435             mergePanelRef = new WeakReference JavaDoc<MergePanel>(panel);
436             getCookieSet().add(new CloseCookieImpl());
437             //activateSave();
438
}
439         
440         private void activateSave() {
441             getCookieSet().add(this);
442         }
443         
444         private void deactivateSave() {
445             getCookieSet().remove(this);
446         }
447         
448         public void propertyChange(PropertyChangeEvent JavaDoc propertyChangeEvent) {
449             if (MergePanel.PROP_CAN_BE_SAVED.equals(propertyChangeEvent.getPropertyName())) {
450                 activateSave();
451             } else if (MergePanel.PROP_CAN_NOT_BE_SAVED.equals(propertyChangeEvent.getPropertyName())) {
452                 deactivateSave();
453             }
454         }
455         
456         public void save() throws java.io.IOException JavaDoc {
457             try {
458                 MergeDialogComponent.this.fireVetoableChange(PROP_PANEL_SAVE, null, mergePanelRef.get());
459             } catch (PropertyVetoException JavaDoc vetoEx) {
460                 Throwable JavaDoc cause = vetoEx.getCause();
461                 if (cause instanceof IOException JavaDoc) {
462                     throw (IOException JavaDoc) cause;
463                 } else {
464                     throw new java.io.IOException JavaDoc(vetoEx.getLocalizedMessage());
465                 }
466             }
467             //System.out.println("SAVE called.");
468
//deactivateSave();
469
}
470         
471         private class CloseCookieImpl extends Object JavaDoc implements CloseCookie {
472             public CloseCookieImpl () {}
473         
474             public boolean close() {
475                 try {
476                     MergeDialogComponent.this.fireVetoableChange(PROP_PANEL_CLOSING, null, mergePanelRef.get());
477                 } catch (PropertyVetoException JavaDoc vetoEx) {
478                     return false;
479                 }
480                 removeMergePanel(mergePanelRef.get());
481                 return true;
482             }
483         }
484         
485     }
486 }
487
Popular Tags