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.bpel.design.decoration.providers;
021:
022: import java.awt.BasicStroke;
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Dimension;
026: import java.awt.Graphics2D;
027: import java.awt.Point;
028: import java.awt.RenderingHints;
029: import java.awt.event.ActionEvent;
030: import java.awt.image.BufferedImage;
031: import java.util.ArrayList;
032: import java.util.Collection;
033: import java.util.List;
034: import javax.swing.AbstractAction;
035: import javax.swing.Action;
036: import javax.swing.Icon;
037: import javax.swing.ImageIcon;
038: import org.netbeans.modules.bpel.design.DesignView;
039: import org.netbeans.modules.bpel.design.DiagramView;
040: import org.netbeans.modules.bpel.design.decoration.ComponentsDescriptor;
041: import org.netbeans.modules.bpel.design.decoration.Decoration;
042: import org.netbeans.modules.bpel.design.decoration.DecorationProvider;
043: import org.netbeans.modules.bpel.design.decoration.Positioner;
044: import org.netbeans.modules.bpel.design.decoration.components.ContextToolBarButton;
045: import org.netbeans.modules.bpel.design.geometry.FPoint;
046: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
047: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
048: import org.netbeans.modules.bpel.design.model.elements.icons.Icon2D;
049: import org.netbeans.modules.bpel.design.model.patterns.CollapsedPattern;
050: import org.netbeans.modules.bpel.design.model.patterns.CompositePattern;
051: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
052: import org.netbeans.modules.bpel.design.selection.DiagramSelectionListener;
053: import org.netbeans.modules.bpel.model.api.ActivityHolder;
054: import org.netbeans.modules.bpel.model.api.BpelContainer;
055: import org.netbeans.modules.bpel.model.api.BpelEntity;
056: import org.netbeans.modules.bpel.model.api.CompositeActivity;
057: import org.netbeans.modules.bpel.model.api.ElseIf;
058: import org.netbeans.modules.bpel.model.api.EventHandlers;
059: import org.netbeans.modules.bpel.model.api.FaultHandlers;
060: import org.netbeans.modules.bpel.model.api.ForEach;
061: import org.netbeans.modules.bpel.model.api.PartnerLink;
062: import org.netbeans.modules.bpel.model.api.Pick;
063: import org.netbeans.modules.bpel.model.api.Process;
064: import org.netbeans.modules.bpel.model.api.support.UniqueId;
065: import org.openide.util.NbBundle;
066:
067: /**
068: *
069: * @author anjeleevich
070: */
071: public class CollapseExpandDecorationProvider extends
072: DecorationProvider
073: // implements DiagramSelectionListener
074: {
075:
076: public CollapseExpandDecorationProvider(DesignView designView) {
077: super (designView);
078:
079: }
080:
081: public Decoration getDecoration(BpelEntity entity) {
082: Pattern pattern = getDesignView().getModel().getPattern(entity);
083:
084: if (pattern == null)
085: return null;
086: if (pattern.getOMReference() == null)
087: return null;
088:
089: if (isInsideCollapsed(entity)) {
090: return null;
091: }
092:
093: if (isCollapsed(entity)) {
094: ComponentsDescriptor components = new ComponentsDescriptor();
095: components.add(new ContextToolBarButton(new ExpandAction(
096: pattern, SMALL_EXPAND_ICON), new Color(0x99FFFFFF,
097: true)), collapseExpandPositioner);
098: return new Decoration(components);
099: }
100:
101: // else if (isCollapsable(entity) && isSelected(entity)) {
102: // ComponentsDescriptor components = new ComponentsDescriptor();
103: // components.add(new JButton(new CollapseAction(pattern)),
104: // collapseExpandPositioner);
105: // return new Decoration(components);
106: // }
107:
108: return null;
109: }
110:
111: // public void selectionChanged(BpelEntity oldSelection,
112: // BpelEntity newSelection)
113: // {
114: // if (oldSelection != null) {
115: // if (isCollapsable(oldSelection) && !isCollapsed(oldSelection)) {
116: // fireDecorationChanged(oldSelection);
117: // }
118: // }
119: //
120: // if (newSelection != null) {
121: // if (isCollapsable(newSelection) && !isCollapsed(newSelection)) {
122: // fireDecorationChanged(newSelection);
123: // }
124: // }
125: // }
126:
127: private boolean isCollapsed(BpelEntity bpelEntity) {
128: return getDesignView().getModel().isCollapsed(bpelEntity);
129: }
130:
131: public boolean isInsideCollapsed(BpelEntity bpelEntity) {
132: for (BpelEntity entity = bpelEntity.getParent(); entity != null; entity = entity
133: .getParent()) {
134: if (isCollapsed(entity))
135: return true;
136: }
137:
138: return false;
139: }
140:
141: public List<BpelEntity> findCollapsedBpelEntitesInside(
142: BpelEntity bpelEntity, List<BpelEntity> result) {
143: if (result == null) {
144: result = new ArrayList<BpelEntity>();
145: }
146:
147: for (BpelEntity child : bpelEntity.getChildren()) {
148: if (isCollapsed(child)) {
149: result.add(child);
150: } else {
151: result = findCollapsedBpelEntitesInside(child, result);
152: }
153: }
154:
155: return result;
156: }
157:
158: private boolean isSelected(BpelEntity bpelEntity) {
159: UniqueId entityID = bpelEntity.getUID();
160: UniqueId selectedEntityID = getDesignView().getSelectionModel()
161: .getSelectedID();
162: return (entityID != null) && entityID.equals(selectedEntityID);
163: }
164:
165: private final Positioner collapseExpandPositioner = new Positioner() {
166: public void position(Pattern pattern,
167: Collection<Component> components, double zoom) {
168: VisualElement ve = null;
169:
170: if (pattern instanceof CompositePattern) {
171: ve = ((CompositePattern) pattern).getBorder();
172: }
173:
174: if (ve == null) {
175: ve = pattern.getFirstElement();
176: }
177:
178: Component component = components.iterator().next();
179:
180: Dimension size = component.getPreferredSize();
181: DiagramView view = pattern.getView();
182: Point center = view.convertDiagramToScreen(new FPoint(ve
183: .getCenterX(), ve.getY() + ve.getHeight()));
184:
185: component.setBounds(center.x - size.width / 2, center.y
186: - size.height - 2, size.width, size.height);
187: }
188: };
189:
190: public Action createCollapseExpandAction(BpelEntity bpelEntity) {
191: Pattern pattern = getDesignView().getModel().getPattern(
192: bpelEntity);
193:
194: if (pattern == null)
195: return null;
196:
197: return createCollapseExpandAction(pattern);
198: }
199:
200: public Action createCollapseExpandAction(Pattern pattern) {
201: if (pattern instanceof CollapsedPattern) {
202: return new ExpandAction((CollapsedPattern) pattern);
203: }
204:
205: if (pattern.isCollapsable()) {
206: return new CollapseAction(pattern);
207: }
208:
209: return null;
210: }
211:
212: private class CollapseAction extends AbstractAction {
213:
214: private Pattern pattern;
215:
216: public CollapseAction(Pattern pattern) {
217: super (NbBundle.getMessage(
218: CollapseExpandDecorationProvider.class,
219: "LBL_COLLAPSE_ACTION"), NORMAL_COLLAPSE_ICON);
220: putValue(SHORT_DESCRIPTION, NbBundle.getMessage(
221: CollapseExpandDecorationProvider.class,
222: "TTT_COLLAPSE_ACTION"));
223:
224: this .pattern = pattern;
225: }
226:
227: public void actionPerformed(ActionEvent event) {
228: pattern.getModel().setCollapsed(pattern.getOMReference(),
229: true);
230: fireDecorationChanged();
231:
232: }
233: }
234:
235: private class ExpandAction extends AbstractAction {
236:
237: private Pattern pattern;
238:
239: public ExpandAction(Pattern pattern, Icon icon) {
240: super (NbBundle.getMessage(
241: CollapseExpandDecorationProvider.class,
242: "LBL_EXPAND_ACTION"), icon);
243: putValue(SHORT_DESCRIPTION, NbBundle.getMessage(
244: CollapseExpandDecorationProvider.class,
245: "TTT_EXPAND_ACTION"));
246:
247: this .pattern = pattern;
248: }
249:
250: public ExpandAction(CollapsedPattern pattern) {
251: this (pattern, NORMAL_EXPAND_ICON);
252: }
253:
254: public void actionPerformed(ActionEvent event) {
255: pattern.getModel().setCollapsed(pattern.getOMReference(),
256: false);
257:
258: fireDecorationChanged();
259: }
260: }
261:
262: private static final Icon SMALL_EXPAND_ICON;
263: private static final Icon NORMAL_EXPAND_ICON;
264: private static final Icon NORMAL_COLLAPSE_ICON;
265:
266: static {
267: BufferedImage image = new BufferedImage(7, 7,
268: BufferedImage.TYPE_INT_ARGB);
269: Graphics2D g2 = image.createGraphics();
270: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
271: RenderingHints.VALUE_ANTIALIAS_ON);
272: g2.setPaint(new Color(0x88FFFFFF, true));
273: g2.fillRect(0, 2, 7, 3);
274: g2.fillRect(2, 0, 3, 7);
275: g2.setPaint(Icon2D.COLOR);
276: g2.setStroke(new BasicStroke(1f));
277: g2.drawLine(0, 3, 6, 3);
278: g2.drawLine(3, 0, 3, 6);
279: g2.dispose();
280: SMALL_EXPAND_ICON = new ImageIcon(image);
281:
282: image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
283: g2 = image.createGraphics();
284: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
285: RenderingHints.VALUE_ANTIALIAS_ON);
286: g2.setPaint(Icon2D.COLOR);
287: g2.fillRect(6, 2, 4, 12);
288: g2.fillRect(2, 6, 12, 4);
289: g2.setPaint(Color.WHITE);
290: g2.fillRect(7, 3, 2, 10);
291: g2.fillRect(3, 7, 10, 2);
292: g2.dispose();
293: NORMAL_EXPAND_ICON = new ImageIcon(image);
294:
295: image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
296: g2 = image.createGraphics();
297: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
298: RenderingHints.VALUE_ANTIALIAS_ON);
299: g2.setPaint(Icon2D.COLOR);
300: g2.fillRect(2, 6, 12, 4);
301: g2.setPaint(Color.WHITE);
302: g2.fillRect(3, 7, 10, 2);
303: g2.dispose();
304: NORMAL_COLLAPSE_ICON = new ImageIcon(image);
305: }
306: }
|