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: package org.netbeans.modules.mobility.svgcore.composer.actions;
042:
043: import java.awt.AWTEvent;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.MouseEvent;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.swing.Action;
049: import org.netbeans.modules.editor.structure.api.DocumentElement;
050: import org.netbeans.modules.mobility.svgcore.SVGDataObject;
051: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerActionFactory;
052: import org.netbeans.modules.mobility.svgcore.composer.ComposerAction;
053: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
054: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
055: import org.netbeans.modules.mobility.svgcore.view.source.SVGSourceMultiViewElement;
056: import org.netbeans.modules.mobility.svgcore.view.svg.AbstractSVGAction;
057: import org.w3c.dom.Node;
058: import org.w3c.dom.svg.SVGLocatableElement;
059:
060: /**
061: *
062: * @author Pavel Benes
063: */
064: public final class SelectActionFactory extends
065: AbstractComposerActionFactory implements
066: SceneManager.SelectionListener {
067: private final AbstractSVGAction m_navigateBackAction = new AbstractSVGAction(
068: "svg_prev_sel", false) { //NOI18N
069: public void actionPerformed(ActionEvent e) {
070: if (m_selectionHistoryIndex > 0) {
071: String[] selection = m_selectionHistory
072: .get(--m_selectionHistoryIndex);
073: m_sceneMgr.setSelection(selection[0], false);
074: updateSelectionHistoryButtons();
075: }
076: }
077: };
078:
079: private final AbstractSVGAction m_navigateForwardAction = new AbstractSVGAction(
080: "svg_next_sel", false, 1) { //NOI18N
081: public void actionPerformed(ActionEvent e) {
082: if (m_selectionHistoryIndex < m_selectionHistory.size() - 1) {
083: String[] selection = m_selectionHistory
084: .get(++m_selectionHistoryIndex);
085: m_sceneMgr.setSelection(selection[0], false);
086: updateSelectionHistoryButtons();
087: }
088: }
089: };
090:
091: private final AbstractSVGAction m_navigateUpAction = new AbstractSVGAction(
092: "svg_parent_sel", false, 2) { //NOI18N
093: public void actionPerformed(ActionEvent e) {
094: SVGObject[] selected = m_sceneMgr.getSelected();
095: if (selected != null && selected.length > 0) {
096: assert selected[0] != null;
097: SVGLocatableElement elem = selected[0].getSVGElement();
098: assert elem != null;
099: Node parent = elem.getParentNode();
100: while (parent != null
101: && !(parent instanceof SVGLocatableElement)) {
102: parent = parent.getParentNode();
103: }
104: if (parent != null) {
105: String elemId = ((SVGLocatableElement) parent)
106: .getId();
107: m_sceneMgr.setSelection(elemId, false);
108: updateSelectionHistory(elemId);
109: }
110: }
111: }
112: };
113:
114: private final List<String[]> m_selectionHistory = new ArrayList<String[]>();
115: private int m_selectionHistoryIndex = -1;
116: private SelectAction m_activeAction;
117:
118: public SelectActionFactory(SceneManager sceneMgr) {
119: super (sceneMgr);
120: }
121:
122: public static MouseEvent getSelectionEvent(AWTEvent evt) {
123: if (evt.getID() == MouseEvent.MOUSE_CLICKED) {
124: MouseEvent me = (MouseEvent) evt;
125: if (me.getButton() == MouseEvent.BUTTON1
126: && me.getClickCount() > 0) {
127: return me;
128: }
129: }
130: return null;
131: }
132:
133: public synchronized ComposerAction startAction(SVGObject selected) {
134: return setActiveAction(new SelectAction(this , selected));
135: }
136:
137: public synchronized ComposerAction startAction(AWTEvent e,
138: boolean isOutsideEvent) {
139: MouseEvent me;
140:
141: if (!isOutsideEvent && (me = getSelectionEvent(e)) != null) {
142: SVGObject[] objects = m_sceneMgr.getPerseusController()
143: .getObjectsAt(me.getX(), me.getY());
144: if (objects != null && objects.length > 0) {
145: String id = objects[0].getElementId();
146:
147: if (me.getClickCount() > 1) {
148: SVGDataObject dObj = m_sceneMgr.getDataObject();
149: DocumentElement delem = dObj.getModel()
150: .getElementById(id);
151: if (delem != null) {
152: SVGSourceMultiViewElement.selectElement(dObj,
153: delem.getStartOffset(), true);
154: }
155: }
156:
157: updateSelectionHistory(id);
158: return setActiveAction(new SelectAction(this ,
159: objects[0]));
160: }
161: }
162: return null;
163: }
164:
165: public SelectAction getActiveAction() {
166: if (m_activeAction != null && !m_activeAction.isCompleted()) {
167: return m_activeAction;
168: } else {
169: return null;
170: }
171: }
172:
173: public Action[] getMenuActions() {
174: return new Action[] { m_navigateBackAction,
175: m_navigateForwardAction, m_navigateUpAction };
176: }
177:
178: public void selectionChanged(SVGObject[] newSelection,
179: SVGObject[] oldSelection, boolean isReadOnly) {
180: boolean navigationUpEnabled = false;
181: if (!m_sceneMgr.isReadOnly()) {
182: if (newSelection != null && newSelection.length > 0) {
183: Node parent = newSelection[0].getSVGElement()
184: .getParentNode();
185: if (parent != null) {
186: navigationUpEnabled = true;
187: }
188: }
189: }
190: m_navigateUpAction.setEnabled(navigationUpEnabled);
191: updateSelectionHistoryButtons();
192: }
193:
194: private SelectAction setActiveAction(SelectAction active) {
195: if (m_activeAction != null) {
196: m_activeAction.actionCompleted();
197: }
198: m_activeAction = active;
199: return active;
200: }
201:
202: private void updateSelectionHistory(String elemId) {
203: String[] selectedIds = new String[] { elemId };
204: for (int i = m_selectionHistory.size() - 1; i > m_selectionHistoryIndex; i--) {
205: m_selectionHistory.remove(i);
206: }
207: m_selectionHistoryIndex = m_selectionHistory.size();
208: m_selectionHistory.add(selectedIds);
209: updateSelectionHistoryButtons();
210: }
211:
212: private void updateSelectionHistoryButtons() {
213: m_navigateBackAction.setEnabled(!m_sceneMgr.isReadOnly()
214: && m_selectionHistoryIndex > 0);
215: m_navigateForwardAction
216: .setEnabled(!m_sceneMgr.isReadOnly()
217: && m_selectionHistoryIndex < m_selectionHistory
218: .size() - 1);
219: }
220: }
|