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.screen;
042:
043: import org.netbeans.modules.vmd.api.model.AccessController;
044: import org.netbeans.modules.vmd.api.model.DesignComponent;
045: import org.netbeans.modules.vmd.api.model.DesignDocument;
046: import org.netbeans.modules.vmd.api.model.DesignEvent;
047: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
048: import org.netbeans.modules.vmd.api.screen.editor.EditedScreenSupport;
049: import org.netbeans.modules.vmd.screen.device.DevicePanel;
050: import org.netbeans.modules.vmd.screen.resource.ResourcePanel;
051:
052: import javax.swing.*;
053: import java.awt.*;
054: import java.awt.event.ActionEvent;
055: import java.awt.event.ActionListener;
056: import java.util.Collection;
057: import java.util.Collections;
058: import java.util.List;
059: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfoPresenter;
060:
061: /**
062: * TODO - implement refresh of only components those were claimed as dirty by their ScreenPresenters - similar to Flow
063: *
064: * @author David Kaspar
065: */
066: public final class ScreenAccessController implements AccessController,
067: EditedScreenSupport.Listener {
068:
069: private final DesignDocument document;
070:
071: private JPanel mainPanel;
072: private DevicePanel devicePanel;
073: private final ResourcePanel resourcePanel;
074:
075: private final JComboBox editedScreenCombo;
076: private final ActionListener editedScreenComboListener;
077:
078: private DesignComponent editedScreen;
079: private List<DesignComponent> allEditableScreens = Collections
080: .emptyList();
081:
082: // Called in document transaction
083: public ScreenAccessController(final DesignDocument document) {
084: this .document = document;
085: resourcePanel = new ResourcePanel(this );
086: editedScreenCombo = new JComboBox();
087: editedScreenCombo.setRenderer(new EditedComboRenderer());
088: editedScreenComboListener = new ActionListener() {
089: public void actionPerformed(ActionEvent e) {
090: document.getTransactionManager().readAccess(
091: new Runnable() {
092: public void run() {
093: DesignComponent component = (DesignComponent) editedScreenCombo
094: .getSelectedItem();
095: EditedScreenSupport
096: .getSupportForDocument(document)
097: .setEditedScreenComponentID(
098: component != null ? component
099: .getComponentID()
100: : -1);
101: }
102: });
103: }
104: };
105: editedScreenCombo.addActionListener(editedScreenComboListener);
106: EditedScreenSupport.getSupportForDocument(document)
107: .addListener(this ); // TODO - maybe possible memory leak, since "this" is an access controller holding design document
108: }
109:
110: public void editedScreenChanged(long editedScreenComponentID) {
111: editedScreen = document.getComponentByUID(EditedScreenSupport
112: .getSupportForDocument(document)
113: .getEditedScreenComponentID());
114: editedScreenCombo
115: .removeActionListener(editedScreenComboListener);
116: editedScreenCombo.setSelectedItem(editedScreen);
117: editedScreenCombo.addActionListener(editedScreenComboListener);
118:
119: refreshPanels();
120: }
121:
122: // Called in document transaction
123: public void writeAccess(Runnable runnable) {
124: runnable.run();
125: }
126:
127: // Called in document transaction
128: public void notifyEventFiring(DesignEvent event) {
129: }
130:
131: // Called in document transaction
132: public void notifyEventFired(DesignEvent event) {
133: //TODO Debug warnig when device presenter is not present
134: if (!isDeviceInfoExists())
135: return;
136: SwingUtilities.invokeLater(new Runnable() {
137: public void run() {
138: document.getTransactionManager().readAccess(
139: new Runnable() {
140: public void run() {
141: refreshModel();
142: }
143: });
144: }
145: });
146: }
147:
148: // called in AWT and document transaction
149: // call this method from notifyEventFired only
150: private void refreshModel() {
151: if (devicePanel == null) {
152: devicePanel = new DevicePanel(this );
153: mainPanel = new MainPanel(devicePanel, resourcePanel);
154: }
155: allEditableScreens = EditedScreenSupport
156: .getAllEditableScreensInDocument(document);
157:
158: editedScreenCombo
159: .removeActionListener(editedScreenComboListener);
160: editedScreenCombo.setModel(new DefaultComboBoxModel(
161: allEditableScreens.toArray()));
162: if (!allEditableScreens.contains(editedScreen))
163: editedScreen = null;
164: editedScreenCombo.setSelectedItem(editedScreen);
165: editedScreenCombo.addActionListener(editedScreenComboListener);
166:
167: if (editedScreen == null && allEditableScreens.size() > 0) {
168: editedScreen = allEditableScreens.get(0);
169: EditedScreenSupport.getSupportForDocument(document)
170: .setEditedScreenComponentID(
171: editedScreen.getComponentID());
172: return;
173: }
174:
175: refreshPanels();
176: }
177:
178: public void refreshPanels() {
179: if (devicePanel == null)
180: return;
181: devicePanel.reload();
182: resourcePanel.reload();
183: mainPanel.validate();
184: }
185:
186: // Called in document transaction
187: public void notifyComponentsCreated(
188: Collection<DesignComponent> createdComponents) {
189: }
190:
191: // Called in AWT
192: void showNotify() {
193: // TODO
194: }
195:
196: // Called in AWT
197: void hideNotify() {
198: // TODO
199: }
200:
201: JComponent getMainPanel() {
202: return mainPanel;
203: }
204:
205: JComponent getToolBar() {
206: return editedScreenCombo;
207: }
208:
209: public DesignDocument getDocument() {
210: return document;
211: }
212:
213: // called in AWT and document transaction
214: public DesignComponent getEditedScreen() {
215: assert SwingUtilities.isEventDispatchThread();
216: assert document.getTransactionManager().isAccess();
217: return editedScreen;
218: }
219:
220: public void setScreenSize(Dimension deviceScreenSize) {
221: if (devicePanel != null)
222: devicePanel.setScreenSize(deviceScreenSize);
223: }
224:
225: private class EditedComboRenderer extends DefaultListCellRenderer {
226:
227: public Component getListCellRendererComponent(JList list,
228: Object value, int index, boolean isSelected,
229: boolean cellHasFocus) {
230: final Image[] image = new Image[1];
231: final String[] label = new String[1];
232: if (value != null) {
233: final DesignComponent dc = (DesignComponent) value;
234: document.getTransactionManager().readAccess(
235: new Runnable() {
236: public void run() {
237: InfoPresenter presenter = dc
238: .getPresenter(InfoPresenter.class);
239: label[0] = presenter
240: .getDisplayName(InfoPresenter.NameType.PRIMARY);
241: image[0] = presenter
242: .getIcon(InfoPresenter.IconType.COLOR_16x16);
243: }
244: });
245: }
246: super .getListCellRendererComponent(list, label[0], index,
247: isSelected, cellHasFocus);
248: if (image[0] != null)
249: setIcon(new ImageIcon(image[0]));
250:
251: return this ;
252: }
253: }
254:
255: private boolean isDeviceInfoExists() {
256: DesignComponent rootComponent = document.getRootComponent();
257: if (rootComponent == null)
258: return false;
259: ScreenDeviceInfoPresenter presenter = rootComponent
260: .getPresenter(ScreenDeviceInfoPresenter.class);
261: if (presenter == null
262: || presenter.getScreenDeviceInfo() == null)
263: return false;
264: return true;
265: }
266:
267: }
|