blob: e64a895e021e24818f55571907c1b003b92319e6 [file] [log] [blame]
nitind35747e32006-02-20 18:52:34 +00001/*******************************************************************************
amywu06123872007-03-16 23:39:27 +00002 * Copyright (c) 2006, 2007 IBM Corporation and others.
nitind35747e32006-02-20 18:52:34 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *
11 *******************************************************************************/
12package org.eclipse.wst.dtd.ui.internal.properties.section;
13
14import java.util.Iterator;
15
16import org.eclipse.swt.SWT;
17import org.eclipse.swt.custom.CCombo;
18import org.eclipse.swt.custom.CLabel;
19import org.eclipse.swt.events.SelectionEvent;
amywu06123872007-03-16 23:39:27 +000020import org.eclipse.swt.graphics.Point;
nitind35747e32006-02-20 18:52:34 +000021import org.eclipse.swt.layout.FormAttachment;
22import org.eclipse.swt.layout.FormData;
23import org.eclipse.swt.widgets.Composite;
nitind003afb72006-04-18 09:03:42 +000024import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
25import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
nitind35747e32006-02-20 18:52:34 +000026import org.eclipse.wst.dtd.core.internal.CMBasicNode;
27import org.eclipse.wst.dtd.core.internal.CMGroupNode;
28import org.eclipse.wst.dtd.core.internal.CMNode;
29import org.eclipse.wst.dtd.core.internal.DTDNode;
30import org.eclipse.wst.dtd.core.internal.Element;
31import org.eclipse.wst.dtd.core.internal.Entity;
32import org.eclipse.wst.dtd.ui.internal.DTDPropertiesMessages;
33
34public class ContentModelTypeSection extends AbstractSection {
35 private final String CONTENT_TYPE = DTDPropertiesMessages._UI_LABEL_CONTENT_TYPE;
36
37 private CCombo typeCombo;
38 private String[] typeComboValues = {CMNode.ANY, CMNode.EMPTY, CMNode.PCDATA, CMNode.CHILDREN, CMNode.MIXED};
39
40 /**
41 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
42 * org.eclipse.wst.common.ui.properties.internal.provisional.TabbedPropertySheetWidgetFactory)
43 */
44 public void createControls(Composite parent, TabbedPropertySheetWidgetFactory factory) {
45 super.createControls(parent, factory);
46 Composite composite = getWidgetFactory().createFlatFormComposite(parent);
amywu58a8dc82006-05-17 21:05:24 +000047
48 // Create label first then attach other control to it
49 CLabel cLabel = getWidgetFactory().createCLabel(composite, CONTENT_TYPE);
amywu06123872007-03-16 23:39:27 +000050
51 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=141106
52 Point p = cLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
53 int labelWidth = Math.max(p.x, 98);
54 FormData data = new FormData(labelWidth, SWT.DEFAULT);
nitind35747e32006-02-20 18:52:34 +000055 data.left = new FormAttachment(0, 0);
nitind35747e32006-02-20 18:52:34 +000056 data.top = new FormAttachment(0, 0);
amywu58a8dc82006-05-17 21:05:24 +000057 cLabel.setLayoutData(data);
nitind35747e32006-02-20 18:52:34 +000058
59 typeCombo = getWidgetFactory().createCCombo(composite, SWT.FLAT);
60 data = new FormData();
amywu58a8dc82006-05-17 21:05:24 +000061 data.left = new FormAttachment(cLabel, -ITabbedPropertyConstants.HSPACE);
62 data.right = new FormAttachment(100);
63 data.top = new FormAttachment(cLabel, 0, SWT.CENTER);
nitind35747e32006-02-20 18:52:34 +000064 typeCombo.setLayoutData(data);
65 typeCombo.addSelectionListener(this);
66 typeCombo.setItems(typeComboValues);
nitind35747e32006-02-20 18:52:34 +000067 }
68
69 /*
70 * @see org.eclipse.wst.common.ui.properties.internal.provisional.view.ITabbedPropertySection#refresh()
71 */
72 public void refresh() {
73 Object input = getInput();
74
75 if (input != null) {
76 if (input instanceof CMNode) {
77 typeCombo.removeAll();
78 typeCombo.add(CMNode.ANY);
79 typeCombo.add(CMNode.EMPTY);
80 typeCombo.add(CMNode.PCDATA);
81 typeCombo.add(CMNode.CHILDREN);
82 typeCombo.add(CMNode.MIXED);
83
84 Iterator iterator = ((CMNode) input).getDTDFile().getNodes().iterator();
85 String nodeName = null;
86 while (iterator.hasNext()) {
87 DTDNode node = (DTDNode) iterator.next();
88 nodeName = node.getName();
89 if (node instanceof Element && typeCombo.indexOf(nodeName) == -1)
90 typeCombo.add(nodeName);
91 else if (node instanceof Entity && ((Entity) node).isParameterEntity() && typeCombo.indexOf(nodeName) == -1)
amywu93ab0962006-04-06 04:38:55 +000092 typeCombo.add("%" + nodeName + ";"); //$NON-NLS-1$ //$NON-NLS-2$
nitind35747e32006-02-20 18:52:34 +000093 }
94 if (input instanceof CMGroupNode)
95 typeCombo.setText(((CMGroupNode) input).getType());
96 else if (input instanceof CMBasicNode)
97 typeCombo.setText(((CMBasicNode) input).getType());
98 }
99 } // end if (fInput != null)
100 }
101
102 public void widgetSelected(SelectionEvent e) {
103 if (e.widget == typeCombo) {
104 Object input = getInput();
105 if (input instanceof CMGroupNode || input instanceof CMBasicNode) {
106 CMNode node = (CMNode) input;
107 String selected = typeCombo.getText();
108 if (CMNode.MIXED.equals(selected))
109 node.setMixedContent();
110 else if (CMNode.CHILDREN.equals(selected))
amywu93ab0962006-04-06 04:38:55 +0000111 node.setChildrenContent(""); //$NON-NLS-1$
nitind35747e32006-02-20 18:52:34 +0000112 else if (CMNode.EMPTY.equals(selected) || CMNode.ANY.equals(selected) || CMNode.PCDATA.equals(selected))
113 node.setContent(selected);
114 else
115 node.setChildrenContent(selected);
116 }
117 }
118 }
119
120 public boolean shouldUseExtraSpace() {
121 return false;
122 }
nitind35747e32006-02-20 18:52:34 +0000123}