001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.merge.builtin.visualizer;
043:
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.Point;
047: import java.awt.Rectangle;
048: import java.awt.Toolkit;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.beans.PropertyVetoException;
052: import java.io.IOException;
053: import java.lang.ref.Reference;
054: import java.lang.ref.WeakReference;
055: import java.util.ArrayList;
056: import java.util.HashMap;
057: import java.util.Iterator;
058: import java.util.Map;
059: import javax.swing.JPopupMenu;
060: import javax.swing.SwingUtilities;
061: import javax.swing.event.ChangeListener;
062: import javax.swing.plaf.TabbedPaneUI;
063: import org.openide.DialogDisplayer;
064: import org.openide.ErrorManager;
065:
066: import org.openide.NotifyDescriptor;
067: import org.openide.actions.FileSystemAction;
068: import org.openide.awt.MouseUtils;
069: import org.openide.awt.JPopupMenuPlus;
070: import org.openide.cookies.CloseCookie;
071: import org.openide.cookies.SaveCookie;
072: import org.openide.nodes.Node;
073: import org.openide.windows.TopComponent;
074: import org.openide.windows.Workspace;
075: import org.openide.util.*;
076: import org.openide.util.actions.CallableSystemAction;
077: import org.openide.util.actions.SystemAction;
078: import org.openide.windows.WindowManager;
079: import org.netbeans.api.javahelp.Help;
080:
081: /**
082: * This is a component, that acts as a non modal dialog.
083: * There are problems with accessibility to non-modal dialogs,
084: * therefore this approach was chosen.
085: * @author Martin Entlicher
086: */
087: public class MergeDialogComponent extends TopComponent implements
088: ChangeListener {
089:
090: public static final String PROP_PANEL_CLOSING = "panelClosing"; // NOI18N
091: public static final String PROP_ALL_CLOSED = "allPanelsClosed"; // NOI18N
092: public static final String PROP_ALL_CANCELLED = "allPanelsCancelled"; // NOI18N
093: public static final String PROP_PANEL_SAVE = "panelSave"; // NOI18N
094:
095: private Map<MergePanel, MergeNode> nodesForPanels = new HashMap<MergePanel, MergeNode>();
096:
097: /** Creates new form MergeDialogComponent */
098: public MergeDialogComponent() {
099: initComponents();
100: initListeners();
101: putClientProperty("PersistenceType", "Never");
102: setName(org.openide.util.NbBundle.getMessage(
103: MergeDialogComponent.class,
104: "MergeDialogComponent.title"));
105: getAccessibleContext().setAccessibleName(
106: NbBundle.getMessage(MergeDialogComponent.class,
107: "ACSN_Merge_Dialog_Component")); // NOI18N
108: getAccessibleContext().setAccessibleDescription(
109: NbBundle.getMessage(MergeDialogComponent.class,
110: "ACSD_Merge_Dialog_Component")); // NOI18N
111: mergeTabbedPane.getAccessibleContext().setAccessibleName(
112: NbBundle.getMessage(MergeDialogComponent.class,
113: "ACSN_Merge_Tabbed_Pane")); // NOI18N
114: mergeTabbedPane.getAccessibleContext()
115: .setAccessibleDescription(
116: NbBundle.getMessage(MergeDialogComponent.class,
117: "ACSD_Merge_Tabbed_Pane")); // NOI18N
118: }
119:
120: public HelpCtx getHelpCtx() {
121: return new HelpCtx(MergeDialogComponent.class);
122: }
123:
124: /** This method is called from within the constructor to
125: * initialize the form.
126: * WARNING: Do NOT modify this code. The content of this method is
127: * always regenerated by the Form Editor.
128: */
129: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
130: private void initComponents() {
131: java.awt.GridBagConstraints gridBagConstraints;
132:
133: mergeTabbedPane = new javax.swing.JTabbedPane();
134: buttonsPanel = new javax.swing.JPanel();
135: okButton = new javax.swing.JButton();
136: cancelButton = new javax.swing.JButton();
137: helpButton = new javax.swing.JButton();
138:
139: FormListener formListener = new FormListener();
140:
141: setLayout(new java.awt.GridBagLayout());
142:
143: mergeTabbedPane.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
144: mergeTabbedPane.setPreferredSize(new java.awt.Dimension(600,
145: 600));
146: gridBagConstraints = new java.awt.GridBagConstraints();
147: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
148: gridBagConstraints.weightx = 1.0;
149: gridBagConstraints.weighty = 1.0;
150: add(mergeTabbedPane, gridBagConstraints);
151:
152: buttonsPanel.setLayout(new java.awt.GridBagLayout());
153:
154: org.openide.awt.Mnemonics.setLocalizedText(okButton,
155: org.openide.util.NbBundle.getMessage(
156: MergeDialogComponent.class, "BTN_OK")); // NOI18N
157: okButton.setToolTipText(org.openide.util.NbBundle.getBundle(
158: MergeDialogComponent.class).getString(
159: "ACS_BTN_OKA11yDesc")); // NOI18N
160: okButton.addActionListener(formListener);
161: gridBagConstraints = new java.awt.GridBagConstraints();
162: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
163: gridBagConstraints.weightx = 1.0;
164: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
165: buttonsPanel.add(okButton, gridBagConstraints);
166:
167: org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
168: org.openide.util.NbBundle.getMessage(
169: MergeDialogComponent.class, "BTN_Cancel")); // NOI18N
170: cancelButton.setToolTipText(org.openide.util.NbBundle
171: .getBundle(MergeDialogComponent.class).getString(
172: "ACS_BTN_CancelA11yDesc")); // NOI18N
173: cancelButton.addActionListener(formListener);
174: gridBagConstraints = new java.awt.GridBagConstraints();
175: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
176: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
177: buttonsPanel.add(cancelButton, gridBagConstraints);
178:
179: org.openide.awt.Mnemonics.setLocalizedText(helpButton,
180: org.openide.util.NbBundle.getMessage(
181: MergeDialogComponent.class, "BTN_Help")); // NOI18N
182: helpButton.setToolTipText(org.openide.util.NbBundle.getBundle(
183: MergeDialogComponent.class).getString(
184: "ACS_BTN_HelpA11yDesc")); // NOI18N
185: helpButton.addActionListener(formListener);
186: gridBagConstraints = new java.awt.GridBagConstraints();
187: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
188: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
189: buttonsPanel.add(helpButton, gridBagConstraints);
190:
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridy = 1;
193: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
194: gridBagConstraints.weightx = 1.0;
195: gridBagConstraints.insets = new java.awt.Insets(12, 12, 11, 11);
196: add(buttonsPanel, gridBagConstraints);
197: }
198:
199: // Code for dispatching events from components to event handlers.
200:
201: private class FormListener implements java.awt.event.ActionListener {
202: FormListener() {
203: }
204:
205: public void actionPerformed(java.awt.event.ActionEvent evt) {
206: if (evt.getSource() == okButton) {
207: MergeDialogComponent.this .okButtonActionPerformed(evt);
208: } else if (evt.getSource() == cancelButton) {
209: MergeDialogComponent.this
210: .cancelButtonActionPerformed(evt);
211: } else if (evt.getSource() == helpButton) {
212: MergeDialogComponent.this
213: .helpButtonActionPerformed(evt);
214: }
215: }
216: }// </editor-fold>//GEN-END:initComponents
217:
218: private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
219: // Add your handling code here:
220: //List panelsToCloseList;
221: Component[] panels;
222: synchronized (this ) {
223: panels = mergeTabbedPane.getComponents();
224: }
225: boolean warning = false;
226: ArrayList<String> unsavedPanelNames = new ArrayList<String>();
227: ArrayList<SaveCookie> saveCookies = new ArrayList<SaveCookie>();
228: for (int i = 0; i < panels.length; i++) {
229: MergePanel panel = (MergePanel) panels[i];
230: if ((panel.getNumUnresolvedConflicts() > 0) && (!warning))
231: warning = true;
232: MergeNode node = nodesForPanels.get(panel);
233: SaveCookie sc;
234: if ((sc = node.getCookie(SaveCookie.class)) != null) {
235: unsavedPanelNames.add(panel.getName());
236: saveCookies.add(sc);
237: }
238: }
239: Object ret;
240: // XXX can format with one format string
241: if (unsavedPanelNames.size() == 1) {
242: ret = DialogDisplayer.getDefault().notify(
243: new NotifyDescriptor.Confirmation(
244: (warning) ? NbBundle.getMessage(
245: MergeDialogComponent.class,
246: "SaveFileWarningQuestion",
247: unsavedPanelNames.get(0))
248: : NbBundle.getMessage(
249: MergeDialogComponent.class,
250: "SaveFileQuestion",
251: unsavedPanelNames.get(0)),
252: NotifyDescriptor.YES_NO_CANCEL_OPTION));
253: } else if (unsavedPanelNames.size() > 1) {
254: ret = DialogDisplayer.getDefault().notify(
255: new NotifyDescriptor.Confirmation(
256: (warning) ? NbBundle.getMessage(
257: MergeDialogComponent.class,
258: "SaveFilesWarningQuestion",
259: new Integer(unsavedPanelNames
260: .size())) : NbBundle
261: .getMessage(
262: MergeDialogComponent.class,
263: "SaveFilesQuestion",
264: new Integer(
265: unsavedPanelNames
266: .size())),
267: NotifyDescriptor.YES_NO_CANCEL_OPTION));
268: } else {
269: if (warning) {
270: ret = DialogDisplayer.getDefault().notify(
271: new NotifyDescriptor.Confirmation(NbBundle
272: .getMessage(MergeDialogComponent.class,
273: "WarningQuestion", new Integer(
274: unsavedPanelNames
275: .size())),
276: NotifyDescriptor.OK_CANCEL_OPTION));
277: if (ret.equals(NotifyDescriptor.NO_OPTION))
278: return;
279: } else
280: ret = NotifyDescriptor.YES_OPTION;
281: }
282: if (!NotifyDescriptor.YES_OPTION.equals(ret)
283: && !NotifyDescriptor.NO_OPTION.equals(ret))
284: return;
285: if (NotifyDescriptor.YES_OPTION.equals(ret)
286: || NotifyDescriptor.OK_OPTION.equals(ret)) {
287: for (SaveCookie sc : saveCookies) {
288: IOException ioException = null;
289: try {
290: sc.save();
291: } catch (UserQuestionException uqex) {
292: Object status = DialogDisplayer
293: .getDefault()
294: .notify(
295: new NotifyDescriptor.Confirmation(
296: uqex.getLocalizedMessage()));
297: if (status == NotifyDescriptor.OK_OPTION
298: || status == NotifyDescriptor.YES_OPTION) {
299: boolean success;
300: try {
301: uqex.confirmed();
302: success = true;
303: } catch (IOException ioex) {
304: success = false;
305: ioException = ioex;
306: }
307: if (success) {
308: try {
309: sc.save();
310: } catch (IOException ioex) {
311: ioException = ioex;
312: }
313: }
314: } else if (status != NotifyDescriptor.NO_OPTION) {
315: // cancel
316: return;
317: }
318: } catch (IOException ioEx) {
319: ioException = ioEx;
320: }
321: if (ioException != null) {
322: ErrorManager.getDefault().notify(ioException);
323: // cancel the close - there was an error on save
324: return;
325: }
326: }
327: }
328: for (int i = 0; i < panels.length; i++) {
329: MergePanel panel = (MergePanel) panels[i];
330: try {
331: fireVetoableChange(PROP_PANEL_CLOSING, null, panel);
332: } catch (PropertyVetoException pvex) {
333: return;
334: }
335: removeMergePanel(panel);
336: }
337: }//GEN-LAST:event_okButtonActionPerformed
338:
339: private void helpButtonActionPerformed(
340: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
341: Help help = (Help) Lookup.getDefault().lookup(Help.class);
342: help.showHelp(getHelpCtx());
343: }//GEN-LAST:event_helpButtonActionPerformed
344:
345: private void cancelButtonActionPerformed(
346: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
347: // Add your handling code here:
348: synchronized (this ) {
349: try {
350: fireVetoableChange(PROP_ALL_CANCELLED, null, null);
351: } catch (PropertyVetoException pvex) {
352: }
353: close();
354: }
355: }//GEN-LAST:event_cancelButtonActionPerformed
356:
357: protected void componentClosed() {
358: Component[] panels;
359: synchronized (this ) {
360: try {
361: fireVetoableChange(PROP_ALL_CANCELLED, null, null);
362: } catch (PropertyVetoException pvex) {
363: }
364:
365: panels = mergeTabbedPane.getComponents();
366: }
367: for (int i = 0; i < panels.length; i++) {
368: MergePanel panel = (MergePanel) panels[i];
369: removeMergePanel(panel);
370: }
371: }
372:
373: /** @return Preferred size of editor top component */
374: public Dimension getPreferredSize() {
375: Rectangle bounds = WindowManager.getDefault()
376: .getCurrentWorkspace().getBounds();
377: return new Dimension(bounds.width / 2,
378: (int) (bounds.height / 1.25));
379: }
380:
381: public int getPersistenceType() {
382: return TopComponent.PERSISTENCE_NEVER;
383: }
384:
385: // Variables declaration - do not modify//GEN-BEGIN:variables
386: private javax.swing.JPanel buttonsPanel;
387: private javax.swing.JButton cancelButton;
388: private javax.swing.JButton helpButton;
389: private javax.swing.JTabbedPane mergeTabbedPane;
390: private javax.swing.JButton okButton;
391:
392: // End of variables declaration//GEN-END:variables
393:
394: public void addNotify() {
395: super .addNotify();
396: javax.swing.JRootPane root = getRootPane();
397: if (root != null)
398: root.setDefaultButton(okButton);
399: /*
400: javax.swing.SwingUtilities.invokeLater(new Runnable() {
401: public void run() {
402: javax.swing.JRootPane root = getRootPane();
403: if (root != null) root.setDefaultButton(okButton);
404: }
405: });
406: */
407: }
408:
409: private void initListeners() {
410: mergeTabbedPane.addMouseListener(new PopupMenuImpl());
411: mergeTabbedPane.addChangeListener(this );
412: }
413:
414: public void open(Workspace workspace) {
415: super .open(workspace);
416: requestActive();
417: }
418:
419: public synchronized void addMergePanel(MergePanel panel) {
420: mergeTabbedPane.addTab(panel.getName(), panel);
421: MergeNode node = new MergeNode(panel);
422: nodesForPanels.put(panel, node);
423: mergeTabbedPane.setSelectedComponent(panel);
424: setActivatedNodes(new Node[] { node });
425: }
426:
427: public synchronized void removeMergePanel(MergePanel panel) {
428: mergeTabbedPane.remove(panel);
429: nodesForPanels.remove(panel);
430: if (mergeTabbedPane.getTabCount() == 0) {
431: try {
432: fireVetoableChange(PROP_ALL_CLOSED, null, null);
433: } catch (PropertyVetoException pvex) {
434: return;
435: }
436: close();
437: }
438: }
439:
440: public MergePanel getSelectedMergePanel() {
441: Component selected = mergeTabbedPane.getSelectedComponent();
442: if (selected == null || !(selected instanceof MergePanel))
443: return null;
444: return ((MergePanel) selected);
445: }
446:
447: private static JPopupMenu createPopupMenu(MergePanel panel) {
448: JPopupMenu popup = new JPopupMenuPlus();
449: SystemAction[] actions = panel.getSystemActions();
450: for (int i = 0; i < actions.length; i++) {
451: if (actions[i] == null) {
452: popup.addSeparator();
453: } else if (actions[i] instanceof CallableSystemAction) {
454: popup.add(((CallableSystemAction) actions[i])
455: .getPopupPresenter());
456: //add FileSystemAction to pop-up menu
457: } else if (actions[i] instanceof FileSystemAction) {
458: popup.add(((FileSystemAction) actions[i])
459: .getPopupPresenter());
460: }
461: }
462: return popup;
463: }
464:
465: /** Shows given popup on given coordinations and takes care about the
466: * situation when menu can exceed screen limits.
467: * Copied from org.netbeans.core.windows.frames.DefaultContainerImpl
468: */
469: private static void showPopupMenu(JPopupMenu popup, Point p,
470: Component comp) {
471: SwingUtilities.convertPointToScreen(p, comp);
472: Dimension popupSize = popup.getPreferredSize();
473: Dimension screenSize = Toolkit.getDefaultToolkit()
474: .getScreenSize();
475:
476: if (p.x + popupSize.width > screenSize.width) {
477: p.x = screenSize.width - popupSize.width;
478: }
479: if (p.y + popupSize.height > screenSize.height) {
480: p.y = screenSize.height - popupSize.height;
481: }
482: SwingUtilities.convertPointFromScreen(p, comp);
483: popup.show(comp, p.x, p.y);
484: }
485:
486: /** Listen on tabbed pane merge panel selection */
487: public void stateChanged(javax.swing.event.ChangeEvent changeEvent) {
488: MergePanel panel = (MergePanel) mergeTabbedPane
489: .getSelectedComponent();
490: if (panel != null) {
491: Node node = nodesForPanels.get(panel);
492: if (node != null) {
493: setActivatedNodes(new Node[] { node });
494: }
495: }
496: }
497:
498: /** Popup menu reaction implementation */
499: private class PopupMenuImpl extends MouseUtils.PopupMouseAdapter {
500: public PopupMenuImpl() {
501: }
502:
503: /** Called when the seqeunce of mouse events should lead to actual
504: * showing of the popup menu. */
505: protected void showPopup(java.awt.event.MouseEvent mouseEvent) {
506: TabbedPaneUI tabUI = mergeTabbedPane.getUI();
507: int clickTab = tabUI.tabForCoordinate(mergeTabbedPane,
508: mouseEvent.getX(), mouseEvent.getY());
509: MergePanel panel = getSelectedMergePanel();
510: if (panel == null) {
511: return;
512: }
513: if (clickTab != -1) {
514: //Click is on valid tab, not on empty area in tab
515: showPopupMenu(createPopupMenu(panel), mouseEvent
516: .getPoint(), mergeTabbedPane);
517: }
518: }
519:
520: }
521:
522: private class MergeNode extends org.openide.nodes.AbstractNode
523: implements PropertyChangeListener, SaveCookie {
524:
525: private Reference<MergePanel> mergePanelRef;
526:
527: public MergeNode(MergePanel panel) {
528: super (org.openide.nodes.Children.LEAF);
529: panel.addPropertyChangeListener(WeakListeners
530: .propertyChange(this , panel));
531: mergePanelRef = new WeakReference<MergePanel>(panel);
532: getCookieSet().add(new CloseCookieImpl());
533: //activateSave();
534: }
535:
536: private void activateSave() {
537: getCookieSet().add(this );
538: }
539:
540: private void deactivateSave() {
541: getCookieSet().remove(this );
542: }
543:
544: public void propertyChange(
545: PropertyChangeEvent propertyChangeEvent) {
546: if (MergePanel.PROP_CAN_BE_SAVED.equals(propertyChangeEvent
547: .getPropertyName())) {
548: activateSave();
549: } else if (MergePanel.PROP_CAN_NOT_BE_SAVED
550: .equals(propertyChangeEvent.getPropertyName())) {
551: deactivateSave();
552: }
553: }
554:
555: public void save() throws java.io.IOException {
556: try {
557: MergeDialogComponent.this .fireVetoableChange(
558: PROP_PANEL_SAVE, null, mergePanelRef.get());
559: } catch (PropertyVetoException vetoEx) {
560: Throwable cause = vetoEx.getCause();
561: if (cause instanceof IOException) {
562: throw (IOException) cause;
563: } else {
564: throw new java.io.IOException(vetoEx
565: .getLocalizedMessage());
566: }
567: }
568: //System.out.println("SAVE called.");
569: //deactivateSave();
570: }
571:
572: private class CloseCookieImpl extends Object implements
573: CloseCookie {
574: public CloseCookieImpl() {
575: }
576:
577: public boolean close() {
578: try {
579: MergeDialogComponent.this .fireVetoableChange(
580: PROP_PANEL_CLOSING, null, mergePanelRef
581: .get());
582: } catch (PropertyVetoException vetoEx) {
583: return false;
584: }
585: removeMergePanel(mergePanelRef.get());
586: return true;
587: }
588: }
589:
590: }
591: }
|