Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 111684b49bc74b1472aab50bad3e19f2cd3b1a45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Code originate from org.eclipse.wst.xml.ui.internal.dialogs
 *******************************************************************************/
package org.eclipse.jpt.jaxb.ui.internal.wizards.classesgen;

import java.util.Collection;
import java.util.List;
import java.util.Vector;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jpt.jaxb.ui.JptJaxbUiPlugin;
import org.eclipse.jpt.jaxb.ui.internal.JptJaxbUiMessages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalog;
import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalogEntry;
import org.eclipse.wst.xml.core.internal.catalog.provisional.INextCatalog;


public class SelectXMLCatalogIdPanel extends Composite {
	protected int catalogEntryType;
	protected boolean doTableSizeHack = false;

	protected XMLCatalogTableViewer tableViewer;
	protected ICatalog fXmlCatalog;

	public SelectXMLCatalogIdPanel(Composite parent, ICatalog xmlCatalog) {
		super(parent, SWT.NONE);
		this.fXmlCatalog = xmlCatalog;

		GridLayout gridLayout = new GridLayout();
		this.setLayout(gridLayout);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = 200;
		gd.widthHint = 700;
		this.setLayoutData(gd);

		Label label = new Label(this, SWT.NONE);
		label.setText(JptJaxbUiMessages.SchemaWizardPage_xmlCatalogTableTitle);

		tableViewer = createTableViewer(this);
		tableViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
		tableViewer.setInput("dummy"); //$NON-NLS-1$
	}

	protected XMLCatalogTableViewer createTableViewer(Composite parent) {
		String headings[] = new String[2];
		headings[0] = JptJaxbUiMessages.SchemaWizardPage_xmlCatalogKeyColumn;
		headings[1] = JptJaxbUiMessages.SchemaWizardPage_xmlCatalogUriColumn;

		XMLCatalogTableViewer theTableViewer = new XMLCatalogTableViewer(parent, headings) {

			protected void addXMLCatalogEntries(List list, ICatalogEntry[] entries) {
				for (int i = 0; i < entries.length; i++) {
					ICatalogEntry entry = entries[i];
					if (catalogEntryType == 0) {
						list.add(entry);
					}
					else if (catalogEntryType == entry.getEntryType()) {
						list.add(entry);
					}
				}
			}

			public Collection getXMLCatalogEntries() {
				List result = null;

				if ((fXmlCatalog == null) || doTableSizeHack) {
					// this lets us create a table with an initial height of
					// 10 rows
					// otherwise we get stuck with 0 row heigh table... that's
					// too small
					doTableSizeHack = false;
					result = new Vector();
					for (int i = 0; i < 6; i++) {
						result.add(""); //$NON-NLS-1$
					}
				}
				else {
					result = new Vector();
					INextCatalog[] nextCatalogs = fXmlCatalog.getNextCatalogs();
					for (int i = 0; i < nextCatalogs.length; i++) {
						INextCatalog catalog = nextCatalogs[i];
						ICatalog referencedCatalog = catalog.getReferencedCatalog();
						if (referencedCatalog != null) {
							if (JptJaxbUiPlugin.SYSTEM_CATALOG_ID.equals(referencedCatalog.getId())) {
								ICatalog systemCatalog = referencedCatalog;
								addXMLCatalogEntries(result, systemCatalog.getCatalogEntries());

							}
							else if (JptJaxbUiPlugin.USER_CATALOG_ID.equals(referencedCatalog.getId())) {
								ICatalog userCatalog = referencedCatalog;
								addXMLCatalogEntries(result, userCatalog.getCatalogEntries());

							}
						}
					}
				}
				return result;
			}
		};
		return theTableViewer;
	}


	public String getId() {
		ICatalogEntry entry = getXMLCatalogEntry();
		return entry != null ? entry.getKey() : null;
	}

	public XMLCatalogTableViewer getTableViewer() {
		return tableViewer;
	}

	public String getURI() {
		ICatalogEntry entry = getXMLCatalogEntry();
		return entry != null ? entry.getURI() : null;
	}

	public ICatalogEntry getXMLCatalogEntry() {
		ICatalogEntry result = null;
		ISelection selection = tableViewer.getSelection();
		Object selectedObject = (selection instanceof IStructuredSelection) ? ((IStructuredSelection) selection).getFirstElement() : null;
		if (selectedObject instanceof ICatalogEntry) {
			result = (ICatalogEntry) selectedObject;
		}
		return result;
	}

	public void setCatalogEntryType(int catalogEntryType) {
		this.catalogEntryType = catalogEntryType;
		tableViewer.refresh();
	}
}

Back to the top