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-2007 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.xslt.project.wizard.element;
042:
043: import java.awt.GridBagConstraints;
044: import java.awt.GridBagLayout;
045: import java.awt.Insets;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.util.ArrayList;
049: import java.util.Collection;
050: import java.util.List;
051:
052: import javax.swing.JButton;
053: import javax.swing.JComboBox;
054: import javax.swing.JLabel;
055: import javax.swing.JPanel;
056: import javax.swing.JTextField;
057:
058: import org.openide.WizardDescriptor;
059: import org.openide.filesystems.FileObject;
060:
061: import org.netbeans.api.project.Project;
062: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
063:
064: import org.netbeans.modules.xml.wsdl.model.Definitions;
065: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
066: import org.netbeans.modules.xml.wsdl.model.Operation;
067: import org.netbeans.modules.xml.wsdl.model.OperationParameter;
068: import org.netbeans.modules.xml.wsdl.model.PortType;
069: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
070: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
071: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
072: import org.netbeans.modules.xml.wsdl.model.visitor.WSDLModelVisitor;
073: import org.netbeans.modules.xml.wsdl.model.visitor.WSDLUtilities;
074: import static org.netbeans.modules.soa.ui.util.UI.*;
075:
076: /**
077: * @author Vladimir Yaroslavskiy
078: * @author Vitaly Bychkov
079: * @version 2007.08.31
080: */
081: final class PanelOperation<T> extends Panel<T> {
082:
083: PanelOperation(Project project, Panel<T> parent, WSDLModel model,
084: String fileName, boolean isReadOnly, boolean isInput) {
085: super (project, parent);
086: myModel = model;
087: myFileName = fileName;
088: myIsReadOnly = isReadOnly;
089: myIsInput = isInput;
090: myIsInputRequired = true;
091: myIsOutputRequired = true;
092: }
093:
094: @Override
095: protected Object getResult() {
096: return getOperation();
097: }
098:
099: @Override
100: protected String getError() {
101: if (myFileName != null) {
102: String fileName = myFile.getText().trim();
103: if (!PanelUtil.isValidFileName(fileName)) {
104: return i18n("ERR_WrongFileName", fileName); // NOI18N
105: }
106:
107: String name = addExtension(fileName);
108: FileObject file = getFolder().getFileObject(name);
109:
110: if (file != null) {
111: return i18n("ERR_File_Already_Exists", name); // NOI18N
112: }
113: }
114: Operation operation = getOperation();
115:
116: if (operation == null) {
117: return i18n("ERR_Operation_Is_Required"); // NOI18N
118: }
119: if (myIsInputRequired) {
120: if (!check(operation.getInput())) {
121: return i18n("ERR_Operation_With_Input_Is_Required"); // NOI18N
122: }
123: }
124: if (myIsOutputRequired) {
125: if (!check(operation.getOutput())) {
126: return i18n("ERR_Operation_With_Output_Is_Required"); // NOI18N
127: }
128: }
129: return null;
130: }
131:
132: private boolean check(OperationParameter parameter) {
133: return parameter != null && parameter.getMessage() != null
134: && parameter.getMessage().get() != null;
135: }
136:
137: @Override
138: public void readSettings(Object object) {
139: myWizardDescriptor = (WizardDescriptor) object;
140: }
141:
142: @Override
143: public void storeSettings(Object object) {
144: WizardDescriptor descriptor = (WizardDescriptor) object;
145:
146: if (myFileName != null) {
147: String file = addExtension(myFile.getText().trim());
148:
149: if (myIsInput) {
150: descriptor.putProperty(INPUT_FILE, file);
151: } else {
152: descriptor.putProperty(OUTPUT_FILE, file);
153: }
154: }
155: if (myIsInput) {
156: descriptor.putProperty(INPUT_OPERATION, getOperation());
157: descriptor.putProperty(INPUT_PARTNER_ROLE_PORT,
158: getPartnerRolePort());
159: } else {
160: descriptor.putProperty(OUTPUT_OPERATION, getOperation());
161: descriptor.putProperty(OUTPUT_PARTNER_ROLE_PORT,
162: getPartnerRolePort());
163: }
164: }
165:
166: void setRequirement(boolean isInputRequired,
167: boolean isOutputRequired) {
168: myIsInputRequired = isInputRequired;
169: myIsOutputRequired = isOutputRequired;
170: }
171:
172: @Override
173: protected void createPanel(JPanel mainPanel, GridBagConstraints cc) {
174: JPanel panel = new JPanel(new GridBagLayout());
175: GridBagConstraints c = new GridBagConstraints();
176: c.anchor = GridBagConstraints.WEST;
177: JButton button;
178: JLabel label;
179:
180: // file
181: createFilePanel(panel, c);
182:
183: // operation
184: c.gridy++;
185: c.gridwidth = 1;
186: c.weightx = 0.0;
187: c.insets = new Insets(TINY_INSET, 0, TINY_INSET, 0);
188: label = createLabel(i18n(myIsInput ? "LBL_Operation"
189: : "LBL_Operation2")); // NOI18N
190: a11y(label, "ACSN_LBL_Operation", "ACSD_LBL_Operation"); // NOI18N
191: panel.add(label, c);
192:
193: c.weightx = 1.0;
194: c.gridwidth = GridBagConstraints.REMAINDER;
195: c.insets = new Insets(TINY_INSET, SMALL_INSET, TINY_INSET, 0);
196: myOperation = new JComboBox();
197: myOperation.setRenderer(new Renderer());
198: myOperation.addActionListener(new ActionListener() {
199: public void actionPerformed(ActionEvent event) {
200: updateTypes();
201: }
202: });
203: label.setLabelFor(myOperation);
204: panel.add(myOperation, c);
205:
206: // [type]
207: if (!myIsReadOnly) {
208: c.weightx = 0.0;
209: createTypePanel(panel, c);
210: }
211:
212: // transform JBI
213: if (myFileName != null) {
214: c.gridy++;
215: c.weightx = 0.0;
216: c.weighty = 1.0;
217: c.insets = new Insets(0, 0, 0, 0);
218: }
219: updatePartnerRolePorts(null);
220: mainPanel.add(panel, cc);
221: }
222:
223: private void createFilePanel(final JPanel panel,
224: GridBagConstraints c) {
225: JLabel label;
226:
227: // xsl file
228: if (myFileName != null) {
229: c.gridy++;
230:
231: GridBagConstraints c1 = new GridBagConstraints();
232: c1.gridy = c.gridy;
233: c1.anchor = GridBagConstraints.WEST;
234: c1.insets = new Insets(TINY_INSET, 0, TINY_INSET, 0);
235: label = createLabel(i18n(myIsInput ? "LBL_XSL_File"
236: : "LBL_XSL_File2")); // NOI18N
237: a11y(label, "ACSN_LBL_XSL_File", "ACSD_LBL_XSL_File"); // NOI18N
238: panel.add(label, c1);
239:
240: c1 = new GridBagConstraints();
241: c1.gridy = c.gridy;
242: c1.insets = new Insets(TINY_INSET, SMALL_INSET, TINY_INSET,
243: 0);
244: c1.fill = GridBagConstraints.HORIZONTAL;
245: c1.weightx = 1.0;
246: myFile = new JTextField(myFileName);
247: label.setLabelFor(myFile);
248: panel.add(myFile, c1);
249:
250: myBrowseButton = createBrowseButton(myFile);
251: c1 = new GridBagConstraints();
252: c1.gridy = c.gridy;
253: c1.insets = new Insets(TINY_INSET, SMALL_INSET, TINY_INSET,
254: 0);
255: panel.add(myBrowseButton, c1);
256: }
257: // Partner/Role/Port
258: c.gridy++;
259: c.gridwidth = 1;
260: c.weightx = 0.0;
261: c.fill = GridBagConstraints.NONE;
262: c.insets = new Insets(TINY_INSET, 0, TINY_INSET, 0);
263: label = createLabel(i18n(myIsInput ? "LBL_Partner_Role_Port"
264: : "LBL_Partner_Role_Port2")); // NOI18N
265: a11y(label, "ACSN_LBL_Partner_Role_Port",
266: "ACSD_LBL_Partner_Role_Port"); // NOI18N
267: panel.add(label, c);
268:
269: c.insets = new Insets(TINY_INSET, SMALL_INSET, TINY_INSET, 0);
270: c.fill = GridBagConstraints.HORIZONTAL;
271: c.weightx = 1.0;
272: c.gridwidth = GridBagConstraints.REMAINDER;
273: myPartnerRolePort = new JComboBox();
274: myPartnerRolePort.setRenderer(new Renderer());
275: myPartnerRolePort.addActionListener(new ActionListener() {
276: public void actionPerformed(ActionEvent event) {
277: update();
278: }
279: });
280: label.setLabelFor(myPartnerRolePort);
281: panel.add(myPartnerRolePort, c);
282: }
283:
284: private void createTypePanel(JPanel panel, GridBagConstraints c) {
285: JLabel label;
286:
287: // input type
288: c.gridy++;
289: c.gridwidth = 1;
290: c.weightx = 0.0;
291: c.insets = new Insets(TINY_INSET, 0, TINY_INSET, 0);
292: label = createLabel(i18n("LBL_Input_Type")); // NOI18N
293: a11y(label, "ACSN_LBL_Input_Type", "ACSD_LBL_Input_Type"); // NOI18N
294: panel.add(label, c);
295:
296: c.insets = new Insets(TINY_INSET, SMALL_INSET, TINY_INSET, 0);
297: c.fill = GridBagConstraints.HORIZONTAL;
298: c.weightx = 1.0;
299: c.gridwidth = GridBagConstraints.REMAINDER;
300: myInput = new JTextField();
301: myInput.setEditable(false);
302: label.setLabelFor(myInput);
303: panel.add(myInput, c);
304:
305: // output type
306: c.gridy++;
307: c.gridwidth = 1;
308: c.weightx = 0.0;
309: c.weighty = 1.0;
310: c.insets = new Insets(TINY_INSET, 0, TINY_INSET, 0);
311: label = createLabel(i18n("LBL_Output_Type")); // NOI18N
312: a11y(label, "ACSN_LBL_Output_Type", "ACSD_LBL_Output_Type"); // NOI18N
313: panel.add(label, c);
314:
315: c.gridwidth = GridBagConstraints.REMAINDER;
316: c.insets = new Insets(TINY_INSET, SMALL_INSET, TINY_INSET, 0);
317: c.fill = GridBagConstraints.HORIZONTAL;
318: myOutput = new JTextField();
319: myOutput.setEditable(false);
320: label.setLabelFor(myOutput);
321: panel.add(myOutput, c);
322: }
323:
324: private void updatePartnerRolePorts(PartnerRolePort partnerRolePort) {
325: myPartnerRolePort.removeAllItems();
326: PartnerRolePort[] partnerRolePorts = getPartnerRolePorts();
327:
328: for (PartnerRolePort item : partnerRolePorts) {
329: myPartnerRolePort.addItem(item);
330: }
331: if (partnerRolePort != null) {
332: myPartnerRolePort.setSelectedItem(partnerRolePort);
333: }
334: update();
335: }
336:
337: private PartnerRolePort[] getPartnerRolePorts() {
338: final List<PartnerRolePort> list = new ArrayList<PartnerRolePort>();
339:
340: WSDLUtilities.visitRecursively(myModel, new WSDLModelVisitor() {
341: public void visit(WSDLModel model) {
342: Definitions definitions = model.getDefinitions();
343: List<ExtensibilityElement> elements = definitions
344: .getExtensibilityElements();
345:
346: for (ExtensibilityElement element : elements) {
347: if (element instanceof PartnerLinkType) {
348: PartnerLinkType partnerLinkType = (PartnerLinkType) element;
349: processRole(partnerLinkType, partnerLinkType
350: .getRole1(), list);
351: processRole(partnerLinkType, partnerLinkType
352: .getRole2(), list);
353: }
354: }
355: }
356: });
357:
358: return list.toArray(new PartnerRolePort[list.size()]);
359: }
360:
361: private void processRole(PartnerLinkType partnerLinkType,
362: Role role, List<PartnerRolePort> list) {
363: if (role == null) {
364: return;
365: }
366: NamedComponentReference<PortType> reference = role
367: .getPortType();
368:
369: if (reference == null) {
370: return;
371: }
372: PortType portType = reference.get();
373:
374: if (portType != null) {
375: PartnerRolePort partnerRolePort = new PartnerRolePort(
376: partnerLinkType, role, portType);
377:
378: if (!list.contains(partnerRolePort)) {
379: list.add(partnerRolePort);
380: }
381: }
382: }
383:
384: @Override
385: protected void update() {
386: myOperation.removeAllItems();
387: Operation[] operations = getOperations(getPartnerRolePort());
388:
389: for (Operation operation : operations) {
390: myOperation.addItem(operation);
391: }
392: updateTypes();
393: }
394:
395: private Operation[] getOperations(PartnerRolePort partnerRolePort) {
396: List<Operation> list = new ArrayList<Operation>();
397:
398: if (partnerRolePort != null) {
399: Collection<Operation> operations = partnerRolePort
400: .getPortType().getOperations();
401:
402: for (Operation operation : operations) {
403: list.add(operation);
404: }
405: }
406: return list.toArray(new Operation[list.size()]);
407: }
408:
409: @Override
410: protected void setEnabled(boolean enabled) {
411: if (myFileName != null) {
412: myFile.setEnabled(enabled);
413: }
414: }
415:
416: private void updateTypes() {
417: getParent().update();
418:
419: if (myIsReadOnly) {
420: return;
421: }
422: Operation operation = getOperation();
423:
424: if (operation == null) {
425: myInput.setText(EMPTY);
426: myOutput.setText(EMPTY);
427: return;
428: }
429: myInput.setText(getType(operation.getInput()));
430: myOutput.setText(getType(operation.getOutput()));
431: }
432:
433: private Operation getOperation() {
434: return (Operation) myOperation.getSelectedItem();
435: }
436:
437: private PartnerRolePort getPartnerRolePort() {
438: return (PartnerRolePort) myPartnerRolePort.getSelectedItem();
439: }
440:
441: private JTextField myFile;
442: private JButton myBrowseButton;
443: private JComboBox myPartnerRolePort;
444: private JComboBox myOperation;
445: private JTextField myInput;
446: private JTextField myOutput;
447: private WSDLModel myModel;
448: private String myFileName;
449: private boolean myIsReadOnly;
450: private boolean myIsInput;
451: private boolean myIsInputRequired;
452: private boolean myIsOutputRequired;
453: private WizardDescriptor myWizardDescriptor;
454:
455: private static final String PART_IN_NAME = "PartIn"; // NOI18N
456: private static final String PART_OUT_NAME = "PartOut"; // NOI18N
457: private static final String MESSAGE_IN_NAME = "MessageIn"; // NOI18N
458: private static final String MESSAGE_OUT_NAME = "MessageOut"; // NOI18N
459: }
|