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.vmd.flow;
042:
043: import org.netbeans.modules.vmd.api.io.DataObjectContext;
044: import org.netbeans.modules.vmd.api.io.DesignDocumentAwareness;
045: import org.netbeans.modules.vmd.api.io.IOUtils;
046: import org.netbeans.modules.vmd.api.model.DesignDocument;
047: import org.netbeans.modules.vmd.api.flow.visual.FlowScene;
048: import org.netbeans.api.visual.widget.Widget;
049: import org.netbeans.api.visual.vmd.VMDMinimizeAbility;
050: import org.openide.util.Utilities;
051: import org.openide.util.NbBundle;
052:
053: import javax.swing.*;
054: import java.awt.*;
055: import java.awt.event.ActionListener;
056: import java.awt.event.ActionEvent;
057:
058: /**
059: * @author David Kaspar
060: */
061: public class FlowViewController implements DesignDocumentAwareness {
062:
063: public static final String FLOW_ID = "flow"; // NOI18N
064:
065: // private DataObjectContext context;
066: private JComponent loadingPanel;
067: private JPanel visual;
068: private JToolBar toolbar;
069:
070: private JButton overviewButton;
071:
072: private DesignDocument designDocument;
073: private JComponent view;
074:
075: public FlowViewController(DataObjectContext context) {
076: // this.context = context;
077: loadingPanel = IOUtils.createLoadingPanel();
078: visual = new JPanel(new BorderLayout()) {
079: @Override
080: public void requestFocus() {
081: super .requestFocus();
082: if (view != null)
083: view.requestFocus();
084: }
085:
086: @Override
087: public boolean requestFocusInWindow() {
088: if (view != null) {
089: super .requestFocusInWindow();
090: return view.requestFocusInWindow();
091: } else
092: return super .requestFocusInWindow();
093: }
094: };
095: // vlv: print
096: visual.putClientProperty(java.awt.print.Printable.class, ""); // NOI18N
097:
098: toolbar = new JToolBar();
099: toolbar.setFloatable(false);
100: toolbar.setRollover(true);
101: toolbar.setPreferredSize(new Dimension(14, 14));
102: toolbar.setSize(new Dimension(14, 14));
103: JToolBar.Separator separator = new JToolBar.Separator();
104: separator.setOrientation(JSeparator.VERTICAL);
105: toolbar.add(separator);
106:
107: addToolbarButton("layout", NbBundle.getMessage(
108: FlowViewController.class, "TTIP_Layout"),
109: new ActionListener() { // NOI18N
110: public void actionPerformed(ActionEvent e) {
111: layout();
112: }
113: });
114:
115: overviewButton = addToolbarButton("overview", NbBundle
116: .getMessage(FlowViewController.class, "TTIP_Overview"),
117: new ActionListener() { // NOI18N
118: public void actionPerformed(ActionEvent e) {
119: overview();
120: }
121: });
122:
123: addToolbarButton("collapse-all", NbBundle.getMessage(
124: FlowViewController.class, "TTIP_CollapseAll"),
125: new ActionListener() { // NOI18N
126: public void actionPerformed(ActionEvent e) {
127: collapseAll();
128: }
129: });
130:
131: addToolbarButton("expand-all", NbBundle.getMessage(
132: FlowViewController.class, "TTIP_ExpandAll"),
133: new ActionListener() { // NOI18N
134: public void actionPerformed(ActionEvent e) {
135: expandAll();
136: }
137: });
138:
139: context.addDesignDocumentAwareness(this );
140: }
141:
142: private JButton addToolbarButton(String imageResourceName,
143: String toolTipText, ActionListener listener) {
144: final JButton button = new JButton(new ImageIcon(Utilities
145: .loadImage("org/netbeans/modules/vmd/flow/resources/"
146: + imageResourceName + ".png"))); // NOI18N
147: button.setOpaque(false);
148: button.setToolTipText(toolTipText);
149: button.setBorderPainted(false);
150: button.setRolloverEnabled(true);
151: button.setSize(14, 14);
152: button.addActionListener(listener);
153: toolbar.add(button);
154: return button;
155: }
156:
157: private void layout() {
158: IOUtils.runInAWTNoBlocking(new Runnable() {
159: public void run() {
160: FlowAccessController accessController = designDocument != null ? designDocument
161: .getListenerManager().getAccessController(
162: FlowAccessController.class)
163: : null;
164: if (accessController == null)
165: return;
166: FlowScene scene = accessController.getScene();
167: scene.layoutScene();
168: scene.validate();
169: }
170: });
171: }
172:
173: private void overview() {
174: DesignDocument doc = designDocument;
175: FlowAccessController accessController = doc != null ? doc
176: .getListenerManager().getAccessController(
177: FlowAccessController.class) : null;
178: if (accessController == null)
179: return;
180:
181: JPopupMenu popup = new JPopupMenu();
182: popup.setLayout(new BorderLayout());
183: JComponent sateliteView = accessController
184: .createSatelliteView();
185: popup.add(sateliteView, BorderLayout.CENTER);
186: popup.show(overviewButton,
187: (overviewButton.getSize().width - sateliteView
188: .getPreferredSize().width) / 2, overviewButton
189: .getSize().height);
190: }
191:
192: private void collapseAll() {
193: IOUtils.runInAWTNoBlocking(new Runnable() {
194: public void run() {
195: FlowAccessController accessController = designDocument != null ? designDocument
196: .getListenerManager().getAccessController(
197: FlowAccessController.class)
198: : null;
199: if (accessController == null)
200: return;
201: FlowScene scene = accessController.getScene();
202: for (Object object : scene.getObjects()) {
203: Widget widget = scene.findWidget(object);
204: if (widget instanceof VMDMinimizeAbility)
205: ((VMDMinimizeAbility) widget).collapseWidget();
206: }
207: }
208: });
209: }
210:
211: private void expandAll() {
212: IOUtils.runInAWTNoBlocking(new Runnable() {
213: public void run() {
214: FlowAccessController accessController = designDocument != null ? designDocument
215: .getListenerManager().getAccessController(
216: FlowAccessController.class)
217: : null;
218: if (accessController == null)
219: return;
220: FlowScene scene = accessController.getScene();
221: for (Object object : scene.getObjects()) {
222: Widget widget = scene.findWidget(object);
223: if (widget instanceof VMDMinimizeAbility)
224: ((VMDMinimizeAbility) widget).expandWidget();
225: }
226: }
227: });
228: }
229:
230: public JComponent getVisualRepresentation() {
231: return visual;
232: }
233:
234: public JComponent getToolbarRepresentation() {
235: return toolbar;
236: }
237:
238: public void setDesignDocument(final DesignDocument newDesignDocument) {
239: IOUtils.runInAWTNoBlocking(new Runnable() {
240: public void run() {
241: designDocument = newDesignDocument;
242: FlowAccessController accessController = designDocument != null ? designDocument
243: .getListenerManager().getAccessController(
244: FlowAccessController.class)
245: : null;
246: view = accessController != null ? accessController
247: .getCreateView() : null;
248:
249: visual.removeAll();
250: if (view != null) {
251: JScrollPane scroll = new JScrollPane(view);
252: scroll.getHorizontalScrollBar()
253: .setUnitIncrement(64);
254: scroll.getHorizontalScrollBar().setBlockIncrement(
255: 256);
256: scroll.getVerticalScrollBar().setUnitIncrement(64);
257: scroll.getVerticalScrollBar()
258: .setBlockIncrement(256);
259: visual.add(scroll, BorderLayout.CENTER);
260: if (visual.hasFocus())
261: view.requestFocus();
262: } else
263: visual.add(loadingPanel, BorderLayout.CENTER);
264: visual.validate();
265: }
266: });
267: }
268:
269: }
|