nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | 0612387 | 2007-03-16 23:39:27 +0000 | [diff] [blame] | 2 | * Copyright (c) 2006, 2007 IBM Corporation and others. |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 3 | * 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 | *******************************************************************************/ |
| 12 | package org.eclipse.wst.dtd.ui.internal.properties.section; |
| 13 | |
| 14 | import java.util.Iterator; |
| 15 | |
| 16 | import org.eclipse.swt.SWT; |
| 17 | import org.eclipse.swt.custom.CCombo; |
| 18 | import org.eclipse.swt.custom.CLabel; |
| 19 | import org.eclipse.swt.events.SelectionEvent; |
amywu | 0612387 | 2007-03-16 23:39:27 +0000 | [diff] [blame] | 20 | import org.eclipse.swt.graphics.Point; |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 21 | import org.eclipse.swt.layout.FormAttachment; |
| 22 | import org.eclipse.swt.layout.FormData; |
| 23 | import org.eclipse.swt.widgets.Composite; |
nitind | 003afb7 | 2006-04-18 09:03:42 +0000 | [diff] [blame] | 24 | import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants; |
| 25 | import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory; |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 26 | import org.eclipse.wst.dtd.core.internal.CMBasicNode; |
| 27 | import org.eclipse.wst.dtd.core.internal.CMGroupNode; |
| 28 | import org.eclipse.wst.dtd.core.internal.CMNode; |
| 29 | import org.eclipse.wst.dtd.core.internal.DTDNode; |
| 30 | import org.eclipse.wst.dtd.core.internal.Element; |
| 31 | import org.eclipse.wst.dtd.core.internal.Entity; |
| 32 | import org.eclipse.wst.dtd.ui.internal.DTDPropertiesMessages; |
| 33 | |
| 34 | public 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); |
amywu | 58a8dc8 | 2006-05-17 21:05:24 +0000 | [diff] [blame] | 47 | |
| 48 | // Create label first then attach other control to it |
| 49 | CLabel cLabel = getWidgetFactory().createCLabel(composite, CONTENT_TYPE); |
amywu | 0612387 | 2007-03-16 23:39:27 +0000 | [diff] [blame] | 50 | |
| 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); |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 55 | data.left = new FormAttachment(0, 0); |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 56 | data.top = new FormAttachment(0, 0); |
amywu | 58a8dc8 | 2006-05-17 21:05:24 +0000 | [diff] [blame] | 57 | cLabel.setLayoutData(data); |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 58 | |
| 59 | typeCombo = getWidgetFactory().createCCombo(composite, SWT.FLAT); |
| 60 | data = new FormData(); |
amywu | 58a8dc8 | 2006-05-17 21:05:24 +0000 | [diff] [blame] | 61 | data.left = new FormAttachment(cLabel, -ITabbedPropertyConstants.HSPACE); |
| 62 | data.right = new FormAttachment(100); |
| 63 | data.top = new FormAttachment(cLabel, 0, SWT.CENTER); |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 64 | typeCombo.setLayoutData(data); |
| 65 | typeCombo.addSelectionListener(this); |
| 66 | typeCombo.setItems(typeComboValues); |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 67 | } |
| 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) |
amywu | 93ab096 | 2006-04-06 04:38:55 +0000 | [diff] [blame] | 92 | typeCombo.add("%" + nodeName + ";"); //$NON-NLS-1$ //$NON-NLS-2$ |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 93 | } |
| 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)) |
amywu | 93ab096 | 2006-04-06 04:38:55 +0000 | [diff] [blame] | 111 | node.setChildrenContent(""); //$NON-NLS-1$ |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 112 | 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 | } |
nitind | 35747e3 | 2006-02-20 18:52:34 +0000 | [diff] [blame] | 123 | } |