001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.php.dbgp.models;
021:
022: import java.awt.Dialog;
023:
024: import javax.swing.Action;
025: import javax.swing.JComponent;
026: import javax.swing.KeyStroke;
027:
028: import org.netbeans.modules.php.dbgp.models.nodes.ScriptWatchEvaluating;
029: import org.netbeans.modules.php.dbgp.models.nodes.VariableNode;
030: import org.netbeans.modules.php.dbgp.ui.WatchPanel;
031: import org.netbeans.spi.viewmodel.Models;
032: import org.netbeans.spi.viewmodel.NodeActionsProvider;
033: import org.netbeans.spi.viewmodel.NodeActionsProviderFilter;
034: import org.netbeans.spi.viewmodel.TreeModel;
035: import org.netbeans.spi.viewmodel.UnknownTypeException;
036: import org.openide.DialogDescriptor;
037: import org.openide.DialogDisplayer;
038: import org.openide.util.HelpCtx;
039: import org.openide.util.NbBundle;
040:
041: /**
042: * @author ads
043: */
044: public class WatchesActionsProvider implements
045: NodeActionsProviderFilter {
046:
047: private static final String DEBUG_ADD_WATCH = "debug.add.watch"; // NOI18N
048:
049: private static final String DIALOG_TITLE = "CTL_WatchDialog_Title"; // NOI18N
050:
051: private static final String WATCH_ACTION_CUSTOMIZE = "CTL_WatchAction_Customize"; // NOI18N
052:
053: private static final String WATCH_ACTION_DELETE = "CTL_WatchAction_Delete"; // NOI18N
054:
055: private static final Action DELETE_ACTION = Models.createAction(
056: NbBundle.getBundle(WatchesActionsProvider.class).getString(
057: WATCH_ACTION_DELETE), new Models.ActionPerformer() {
058: public boolean isEnabled(Object node) {
059: return true;
060: }
061:
062: public void perform(Object[] nodes) {
063: for (Object node : nodes) {
064: ((ScriptWatchEvaluating) node).remove();
065: }
066: }
067: }, Models.MULTISELECTION_TYPE_ANY);
068:
069: static {
070: DELETE_ACTION.putValue(Action.ACCELERATOR_KEY, KeyStroke
071: .getKeyStroke("DELETE") // NOI18N
072: );
073: };
074:
075: private static final Action CUSTOMIZE_ACTION = Models.createAction(
076: NbBundle.getBundle(WatchesActionsProvider.class).getString(
077: WATCH_ACTION_CUSTOMIZE),
078: new Models.ActionPerformer() {
079: public boolean isEnabled(Object node) {
080: return true;
081: }
082:
083: public void perform(Object[] nodes) {
084: customize((ScriptWatchEvaluating) nodes[0]);
085: }
086: }, Models.MULTISELECTION_TYPE_EXACTLY_ONE);
087:
088: /* (non-Javadoc)
089: * @see org.netbeans.spi.viewmodel.NodeActionsProviderFilter#getActions(org.netbeans.spi.viewmodel.NodeActionsProvider, java.lang.Object)
090: */
091: public Action[] getActions(NodeActionsProvider original, Object node)
092: throws UnknownTypeException {
093: Action[] actions;
094: try {
095: actions = original.getActions(node);
096: } catch (UnknownTypeException e) {
097: actions = new Action[0];
098: }
099: Action[] varActions = getActions(node);
100: Action[] result = new Action[actions.length + varActions.length];
101: System.arraycopy(actions, 0, result, 0, actions.length);
102: System.arraycopy(varActions, 0, result, actions.length,
103: varActions.length);
104: return result;
105: }
106:
107: /* (non-Javadoc)
108: * @see org.netbeans.spi.viewmodel.NodeActionsProviderFilter#performDefaultAction(org.netbeans.spi.viewmodel.NodeActionsProvider, java.lang.Object)
109: */
110: public void performDefaultAction(NodeActionsProvider original,
111: Object node) throws UnknownTypeException {
112: if (node instanceof ScriptWatchEvaluating) {
113: performDefaultAction(node);
114: } else {
115: original.performDefaultAction(node);
116: }
117: }
118:
119: private Action[] getActions(Object node)
120: throws UnknownTypeException {
121: if (node == TreeModel.ROOT) {
122: return new Action[0];
123: }
124: if (node instanceof ScriptWatchEvaluating) {
125: return new Action[] { DELETE_ACTION, null, CUSTOMIZE_ACTION };
126: }
127:
128: throw new UnknownTypeException(node);
129: }
130:
131: private void performDefaultAction(Object node)
132: throws UnknownTypeException {
133: if (node == TreeModel.ROOT || node instanceof VariableNode) {
134: return;
135: }
136: throw new UnknownTypeException(node);
137: }
138:
139: /*
140: * Stolen from org.netbeans.modules.debugger.ui.models.WatchesActionsProvider
141: */
142: private static void customize(ScriptWatchEvaluating watchEvaluating) {
143: WatchPanel watchPanel = new WatchPanel(watchEvaluating
144: .getExpression());
145: JComponent panel = watchPanel.getPanel();
146:
147: DialogDescriptor descriptor = new DialogDescriptor(panel,
148: NbBundle.getMessage(WatchesActionsProvider.class,
149: DIALOG_TITLE, watchEvaluating.getExpression()));
150: descriptor.setHelpCtx(new HelpCtx(DEBUG_ADD_WATCH));
151: Dialog dialog = DialogDisplayer.getDefault().createDialog(
152: descriptor);
153: dialog.setVisible(true);
154: dialog.dispose();
155:
156: if (descriptor.getValue() != org.openide.DialogDescriptor.OK_OPTION) {
157: return;
158: }
159: watchEvaluating.setExpression(watchPanel.getExpression());
160: }
161: }
|