PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wlm.editor/src/org/netbeans/modules/worklist/editor/designview/ActionPanel.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 428 lines | 301 code | 82 blank | 45 comment | 19 complexity | a2be17b72c41aa5089ceb4d9624b8b9b MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 1997-2009 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.worklist.editor.designview;
  43. import java.awt.Color;
  44. import java.awt.Dimension;
  45. import java.awt.Insets;
  46. import java.awt.Rectangle;
  47. import java.awt.event.ActionEvent;
  48. import java.awt.event.ActionListener;
  49. import java.awt.event.FocusEvent;
  50. import java.awt.event.FocusListener;
  51. import javax.swing.AbstractAction;
  52. import javax.swing.JComboBox;
  53. import javax.swing.JComponent;
  54. import javax.swing.JLabel;
  55. import org.netbeans.modules.wlm.model.api.TAction;
  56. import org.netbeans.modules.wlm.model.api.TActionType;
  57. import org.netbeans.modules.wlm.model.api.TTask;
  58. import org.netbeans.modules.wlm.model.api.WLMComponent;
  59. import org.netbeans.modules.wlm.model.api.WLMModel;
  60. import org.netbeans.modules.worklist.editor.designview.components.ExUtils;
  61. import org.netbeans.modules.worklist.editor.designview.components.TextFieldEditor;
  62. import org.netbeans.modules.worklist.editor.designview.components.TitledPanel;
  63. import org.netbeans.modules.worklist.editor.nodes.ActionNode;
  64. import org.netbeans.modules.worklist.editor.nodes.WLMNodeType;
  65. import org.openide.nodes.Children;
  66. import org.openide.nodes.Node;
  67. import org.openide.util.NbBundle;
  68. /**
  69. *
  70. * @author anjeleevich
  71. */
  72. public class ActionPanel extends DesignViewPanel implements Widget,
  73. FocusListener
  74. {
  75. private TAction action;
  76. private RemoveActionAction removeActionAction;
  77. private TitledPanel titledPanel;
  78. private JLabel nameLabel;
  79. private JLabel typeLabel;
  80. private NameEditor nameEditor;
  81. private TypeChooser typeChooser;
  82. private LocalNotificationsPanel localNotificationsPanel;
  83. private Widget widgetParent;
  84. public ActionPanel(Widget widgetParent, DesignView designView,
  85. TAction action)
  86. {
  87. super(designView);
  88. ExUtils.setA11Y(this, "ActionPanel"); // NOI18N
  89. setBorder(null);
  90. setBackground(TitledPanel.BACKGROUND_COLOR);
  91. setOpaque(true);
  92. this.widgetParent = widgetParent;
  93. this.action = action;
  94. removeActionAction = new RemoveActionAction();
  95. titledPanel = new TitledPanel(getMessage("LBL_ACTION"), // NOI18N
  96. removeActionAction, this, 0); // NOI18N
  97. ExUtils.setA11Y(titledPanel, ActionPanel.class,
  98. "ActionTitlePanel"); // NOI18N
  99. nameLabel = new JLabel(getMessage("LBL_ACTION_NAME")); // NOI18N
  100. ExUtils.setA11Y(nameLabel, ActionPanel.class,
  101. "ActionNameLabel"); // NOI18N
  102. typeLabel = new JLabel(getMessage("LBL_ACTION_TYPE")); // NOI18N
  103. ExUtils.setA11Y(typeLabel, ActionPanel.class,
  104. "ActionTypeLabel"); // NOI18N
  105. nameEditor = new NameEditor(designView);
  106. ExUtils.setA11Y(nameEditor, ActionPanel.class,
  107. "ActionNameEditor"); // NOI18N
  108. typeChooser = new TypeChooser();
  109. typeChooser.addFocusListener(this);
  110. ExUtils.setA11Y(typeChooser, ActionPanel.class,
  111. "ActionTypeChooser"); // NOI18N
  112. localNotificationsPanel = new LocalNotificationsPanel(this, designView,
  113. action);
  114. add(nameLabel);
  115. add(nameEditor);
  116. add(typeLabel);
  117. add(typeChooser);
  118. add(localNotificationsPanel);
  119. }
  120. public TAction getAction() {
  121. return action;
  122. }
  123. public JComponent getView() {
  124. return titledPanel;
  125. }
  126. public void processWLMModelChanged() {
  127. nameEditor.updateContent();
  128. typeChooser.updateContent();
  129. localNotificationsPanel.processWLMModelChanged();
  130. }
  131. public void activateNode() {
  132. Node node = new ActionNode(getAction(), Children.LEAF,
  133. getNodeLookup());
  134. getDesignView().setActivatedNode(node);
  135. }
  136. @Override
  137. public Dimension getPreferredSize() {
  138. synchronized (getTreeLock()) {
  139. Dimension size11 = nameLabel.getPreferredSize();
  140. Dimension size12 = nameEditor.getPreferredSize();
  141. Dimension size21 = typeLabel.getPreferredSize();
  142. Dimension size22 = typeChooser.getPreferredSize();
  143. Dimension notificationSize = localNotificationsPanel
  144. .getPreferredSize();
  145. Insets insets = getInsets();
  146. int col1 = ExUtils.maxWidth(size11, size21);
  147. int col2 = ExUtils.maxWidth(size12, size22);
  148. int row1 = ExUtils.maxHeight(size11, size12);
  149. int row2 = ExUtils.maxHeight(size21, size22);
  150. int w = Math.max(col1 + HGAP1 + col2, notificationSize.width);
  151. int h = row1 + VGAP1 + row2 + VGAP2 + notificationSize.height;
  152. w += insets.left + insets.right;
  153. h += insets.top + insets.bottom;
  154. return new Dimension(w, h);
  155. }
  156. }
  157. @Override
  158. public void doLayout() {
  159. synchronized (getTreeLock()) {
  160. Insets insets = getInsets();
  161. int x = insets.left;
  162. int w = getWidth() - x - insets.right;
  163. int y = insets.top;
  164. int h = getHeight() - y - insets.bottom;
  165. Dimension size11 = nameLabel.getPreferredSize();
  166. Dimension size12 = nameEditor.getPreferredSize();
  167. Dimension size21 = typeLabel.getPreferredSize();
  168. Dimension size22 = typeChooser.getPreferredSize();
  169. Dimension notificationSize = localNotificationsPanel
  170. .getPreferredSize();
  171. int col1 = ExUtils.maxWidth(size11, size21);
  172. int col2 = ExUtils.maxWidth(size12, size22);
  173. col2 = Math.max(col2, (w - col1 - HGAP1) / 2);
  174. col2 = Math.min(col2, w - col1 - HGAP1);
  175. int row1 = ExUtils.maxHeight(size11, size12);
  176. int row2 = ExUtils.maxHeight(size21, size22);
  177. int x2 = x + col1 + HGAP1;
  178. int y2 = y + row1 + VGAP1;
  179. int y3 = y2 + row2 + VGAP2;
  180. nameLabel.setBounds(x, y, Math.min(col1, size11.width), row1);
  181. nameEditor.setBounds(x2, y, col2, row1);
  182. typeLabel.setBounds(x, y2, Math.min(col1, size21.width), row2);
  183. typeChooser.setBounds(x2, y2, col2, row2);
  184. h -= row1 + VGAP1 + row2 + VGAP2;
  185. localNotificationsPanel.setBounds(x, y3, w, h);
  186. }
  187. }
  188. public Widget getWidgetParent() {
  189. return widgetParent;
  190. }
  191. public Widget getWidget(int index) {
  192. if (index == 0) {
  193. return localNotificationsPanel;
  194. }
  195. throw new IndexOutOfBoundsException();
  196. }
  197. public int getWidgetCount() {
  198. return 1;
  199. }
  200. public Node getWidgetNode() {
  201. return new ActionNode(action, Children.LEAF, getNodeLookup());
  202. }
  203. public void requestFocusToWidget() {
  204. getDesignView().showActionsTab();
  205. scrollRectToVisible(new Rectangle(getSize()));
  206. if (!typeChooser.hasFocus()) {
  207. nameEditor.requestFocusInWindow();
  208. }
  209. }
  210. public WLMComponent getWidgetWLMComponent() {
  211. return action;
  212. }
  213. public WLMNodeType getWidgetNodeType() {
  214. return WLMNodeType.ACTION;
  215. }
  216. public void focusGained(FocusEvent e) {
  217. selectWidget(this);
  218. }
  219. public void focusLost(FocusEvent e) {
  220. }
  221. private class RemoveActionAction extends AbstractAction {
  222. RemoveActionAction() {
  223. super("Remove"); // NOI18N
  224. }
  225. public void actionPerformed(ActionEvent event) {
  226. WLMModel model = getModel();
  227. TTask task = getTask();
  228. if (model.startTransaction()) {
  229. try {
  230. task.removeAction(action);
  231. } finally {
  232. model.endTransaction();
  233. }
  234. }
  235. }
  236. }
  237. private class NameEditor extends TextFieldEditor {
  238. public NameEditor(DesignView designView) {
  239. super(designView, false);
  240. }
  241. @Override
  242. public String getModelValue() {
  243. String name = action.getName();
  244. if (name == null) {
  245. name = ""; // NOI18N
  246. } else {
  247. name = name.trim();
  248. }
  249. return name;
  250. }
  251. @Override
  252. public void setModelValue(String value) {
  253. if (value == null) {
  254. value = ""; // NOI18N
  255. } else {
  256. value = value.trim();
  257. }
  258. WLMModel model = getModel();
  259. if (model.startTransaction()) {
  260. try {
  261. action.setName(value);
  262. } finally {
  263. model.endTransaction();
  264. }
  265. }
  266. }
  267. @Override
  268. public void activateNode() {
  269. selectWidget(ActionPanel.this);
  270. }
  271. }
  272. private class TypeChooser extends JComboBox {
  273. private ActionListener actionListener;
  274. TypeChooser() {
  275. TActionType currentActionType = getModelValue();
  276. ActionTypeWrapper toSelect = null;
  277. TActionType[] actionTypes = TActionType.values();
  278. for (TActionType actionType : actionTypes) {
  279. ActionTypeWrapper wrapper = new ActionTypeWrapper(actionType);
  280. addItem(wrapper);
  281. if (actionType == currentActionType) {
  282. toSelect = wrapper;
  283. }
  284. }
  285. setSelectedItem(toSelect);
  286. actionListener = new ActionListener() {
  287. public void actionPerformed(ActionEvent e) {
  288. ActionTypeWrapper wrapper = (ActionTypeWrapper)
  289. getSelectedItem();
  290. TActionType actionType = (wrapper == null) ? null
  291. : wrapper.getActionType();
  292. setModelValue(actionType);
  293. }
  294. };
  295. addActionListener(actionListener);
  296. }
  297. public void updateContent() {
  298. removeActionListener(actionListener);
  299. TActionType currentActionType = getModelValue();
  300. for (int i = getItemCount() - 1; i >= 0; i--) {
  301. ActionTypeWrapper item = (ActionTypeWrapper) getItemAt(i);
  302. if (item.getActionType() == currentActionType) {
  303. setSelectedItem(item);
  304. addActionListener(actionListener);
  305. return;
  306. }
  307. }
  308. setSelectedItem(null);
  309. addActionListener(actionListener);
  310. }
  311. public TActionType getModelValue() {
  312. return action.getType();
  313. }
  314. public void setModelValue(TActionType actionType) {
  315. WLMModel model = ActionPanel.this.getModel();
  316. TAction action = ActionPanel.this.getAction();
  317. if (model.startTransaction()) {
  318. try {
  319. action.setType(actionType);
  320. } finally {
  321. model.endTransaction();
  322. }
  323. }
  324. }
  325. }
  326. private static class ActionTypeWrapper {
  327. private TActionType actionType;
  328. ActionTypeWrapper(TActionType actionType) {
  329. this.actionType = actionType;
  330. }
  331. TActionType getActionType() {
  332. return actionType;
  333. }
  334. @Override
  335. public String toString() {
  336. return actionType.value();
  337. }
  338. }
  339. private static final int HGAP1 = 8;
  340. private static final int HGAP2 = 4;
  341. private static final int VGAP1 = 4;
  342. private static final int VGAP2 = 8;
  343. private static String getMessage(String key) {
  344. return NbBundle.getMessage(ActionPanel.class, key);
  345. }
  346. }