001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder.ui;
022
023 import java.util.HashSet;
024 import java.util.Set;
025
026 import org.eclipse.core.runtime.CoreException;
027 import org.eclipse.swt.SWT;
028 import org.eclipse.swt.events.SelectionAdapter;
029 import org.eclipse.swt.events.SelectionEvent;
030 import org.eclipse.swt.graphics.Rectangle;
031 import org.eclipse.swt.layout.FillLayout;
032 import org.eclipse.swt.layout.GridData;
033 import org.eclipse.swt.layout.GridLayout;
034 import org.eclipse.swt.widgets.Button;
035 import org.eclipse.swt.widgets.Composite;
036 import org.eclipse.swt.widgets.Label;
037 import org.eclipse.swt.widgets.Tree;
038 import org.eclipse.swt.widgets.TreeItem;
039 import org.granite.builder.GraniteBuilderContext;
040 import org.granite.builder.properties.Gas3Template;
041 import org.granite.builder.properties.GraniteProperties;
042 import org.granite.builder.util.SWTUtil;
043 import org.granite.generator.TemplateUri;
044 import org.granite.generator.as3.DefaultAs3TypeFactory;
045 import org.granite.generator.as3.LCDSAs3TypeFactory;
046 import org.granite.generator.as3.reflect.JavaType.Kind;
047 import org.granite.generator.template.StandardTemplateUris;
048
049 /**
050 * @author Franck WOLFF
051 */
052 public class TemplatesPanel extends Composite {
053
054 private final GraniteProperties properties;
055
056 private Tree templatesTree = null;
057 private boolean initialized = false;
058
059 public TemplatesPanel(Composite parent, GraniteBuilderContext context) throws CoreException {
060 super(parent, SWT.NONE);
061 this.properties = context.getProperties();
062 initializeComponents();
063 }
064
065 public Set<Gas3Template> getTemplates() {
066 if (!initialized)
067 return properties.getGas3().getTemplates();
068
069 Set<Gas3Template> templates = new HashSet<Gas3Template>(templatesTree.getItemCount());
070 for (TreeItem kindItem : templatesTree.getItems()) {
071 StringBuilder sb = new StringBuilder();
072 if (kindItem.getItemCount() > 0)
073 sb.append((String)kindItem.getItem(0).getData());
074 if (kindItem.getItemCount() > 1)
075 sb.append(';').append((String)kindItem.getItem(1).getData());
076 templates.add(new Gas3Template((Kind)kindItem.getData(), sb.toString()));
077 }
078 return templates;
079 }
080
081 @Override
082 public Rectangle getClientArea() {
083 initializeContent();
084 return super.getClientArea();
085 }
086
087 private void initializeContent() {
088 if (!initialized) {
089 for (Kind kind : Kind.values()) {
090 TreeItem kindItem = SWTUtil.addTreeItem(templatesTree, SWTUtil.IMG_TEMPLATE, kind.name(), null, null);
091 kindItem.setData(kind);
092 TemplateUri[] uris = properties.getGas3().getMatchingTemplateUris(kind);
093 for (TemplateUri uri : uris) {
094 TreeItem uriItem = SWTUtil.addTreeItem(kindItem, SWTUtil.IMG_FILE, uri.getUri() + (uri.isBase() ? " (base)" : ""), null, null);
095 uriItem.setData(uri.getUri());
096 }
097 kindItem.setExpanded(true);
098 }
099 initialized = true;
100 }
101 }
102
103 private void initializeComponents() {
104 setLayout(new GridLayout(2, false));
105
106 Label text = new Label(this, SWT.NONE);
107 text.setText("Templates used for generation:");
108 text.setLayoutData(SWTUtil.newGridData(SWT.NONE, 2));
109
110 templatesTree = new Tree(this, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
111 templatesTree.setLayoutData(new GridData(
112 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL |
113 GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL
114 ));
115
116
117 Composite buttons = new Composite(this, SWT.NONE);
118 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
119 buttons.setLayout(new FillLayout(SWT.VERTICAL));
120
121 final Button editButton = SWTUtil.newButton(buttons, "Edit...", false, new SelectionAdapter() {
122 @Override
123 public void widgetSelected(SelectionEvent e) {
124 editTemplatesHandler(e);
125 }
126 });
127
128 final Button removeButton = SWTUtil.newButton(buttons, "Remove", false, new SelectionAdapter() {
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 removeTemplatesHandler(e);
132 }
133 });
134
135 SWTUtil.newButton(buttons, "Reset to default", true, new SelectionAdapter() {
136 @Override
137 public void widgetSelected(SelectionEvent e) {
138 properties.getGas3().setAs3TypeFactory(DefaultAs3TypeFactory.class.getName());
139
140 properties.getGas3().getTemplates().clear();
141 properties.getGas3().getTemplates().addAll(GraniteProperties.getDefaultProperties().getGas3().getTemplates());
142
143 for (TreeItem item : templatesTree.getItems())
144 item.dispose();
145
146 initialized = false;
147 initializeContent();
148 }
149 });
150
151 SWTUtil.newButton(buttons, "Use Tide", true, new SelectionAdapter() {
152 @Override
153 public void widgetSelected(SelectionEvent e) {
154 properties.getGas3().setAs3TypeFactory(DefaultAs3TypeFactory.class.getName());
155
156 Gas3Template template = properties.getGas3().getTemplate(Kind.ENTITY);
157 template.setUri(StandardTemplateUris.ENTITY, false);
158 template.setUri(StandardTemplateUris.TIDE_ENTITY_BASE, true);
159
160 template = properties.getGas3().getTemplate(Kind.REMOTE_DESTINATION);
161 template.setUri(StandardTemplateUris.REMOTE, false);
162 template.setUri(StandardTemplateUris.TIDE_REMOTE_BASE, true);
163
164 template = properties.getGas3().getTemplate(Kind.BEAN);
165 template.setUri(StandardTemplateUris.BEAN, false);
166 template.setUri(StandardTemplateUris.TIDE_BEAN_BASE, true);
167
168 template = properties.getGas3().getTemplate(Kind.ENUM);
169 template.setUri(StandardTemplateUris.ENUM, false);
170
171 for (TreeItem item : templatesTree.getItems())
172 item.dispose();
173
174 initialized = false;
175 initializeContent();
176 }
177 });
178
179 SWTUtil.newButton(buttons, "Use LCDS", true, new SelectionAdapter() {
180 @Override
181 public void widgetSelected(SelectionEvent e) {
182 properties.getGas3().setAs3TypeFactory(LCDSAs3TypeFactory.class.getName());
183
184 Gas3Template template = properties.getGas3().getTemplate(Kind.ENTITY);
185 template.setUri(StandardTemplateUris.BEAN, false);
186 template.setUri(StandardTemplateUris.LCDS_BEAN_BASE, true);
187
188 template = properties.getGas3().getTemplate(Kind.REMOTE_DESTINATION);
189 template.setUris("");
190
191 template = properties.getGas3().getTemplate(Kind.BEAN);
192 template.setUri(StandardTemplateUris.BEAN, false);
193 template.setUri(StandardTemplateUris.LCDS_BEAN_BASE, true);
194
195 template = properties.getGas3().getTemplate(Kind.ENUM);
196 template.setUris("");
197
198 for (TreeItem item : templatesTree.getItems())
199 item.dispose();
200
201 initialized = false;
202 initializeContent();
203 }
204 });
205
206 templatesTree.addSelectionListener(new SelectionAdapter() {
207 @Override
208 public void widgetSelected(SelectionEvent e) {
209 // Enable/Disable buttons based on selected tree item.
210 editButton.setEnabled(templatesTree.getSelection() != null && templatesTree.getSelection().length > 0);
211 removeButton.setEnabled(templatesTree.getSelection() != null && templatesTree.getSelection().length > 0);
212 }
213 });
214 }
215
216 private void editTemplatesHandler(SelectionEvent e) {
217 if (templatesTree.getSelection() == null || templatesTree.getSelection().length == 0)
218 return;
219
220 TreeItem kindItem = templatesTree.getSelection()[0];
221 while (kindItem.getParentItem() != null)
222 kindItem = kindItem.getParentItem();
223
224 String templateUri = (kindItem.getItemCount() <= 0 ? "" : (String)kindItem.getItems()[0].getData());
225 String baseTemplateUri = (kindItem.getItemCount() <= 1 ? "" : (String)kindItem.getItems()[1].getData());
226
227 String[] uris = Dialogs.editTemplateUris(
228 getDisplay().getActiveShell(),
229 "Templates for " + kindItem.getText() + " type",
230 templateUri,
231 baseTemplateUri
232 );
233
234 if (uris != null) {
235 if (kindItem.getItemCount() > 0) {
236 kindItem.getItem(0).setText(uris[0]);
237 kindItem.getItem(0).setData(uris[0]);
238 }
239 else {
240 TreeItem uriItem = SWTUtil.addTreeItem(kindItem, SWTUtil.IMG_FILE, uris[0], null, null);
241 uriItem.setData(uris[0]);
242 }
243
244 if (uris[1].length() > 0) {
245 if (kindItem.getItemCount() > 1) {
246 kindItem.getItem(1).setText(uris[1]);
247 kindItem.getItem(1).setData(uris[1]);
248 }
249 else {
250 TreeItem uriItem = SWTUtil.addTreeItem(kindItem, SWTUtil.IMG_FILE, uris[1], null, null);
251 uriItem.setData(uris[1]);
252 }
253 }
254 else if (kindItem.getItemCount() > 1)
255 kindItem.getItem(1).dispose();
256 }
257 }
258
259 private void removeTemplatesHandler(SelectionEvent e) {
260 if (templatesTree.getSelection() == null || templatesTree.getSelection().length == 0)
261 return;
262
263 TreeItem kindItem = templatesTree.getSelection()[0];
264 if (kindItem.getParentItem() == null) {
265 for (TreeItem child : kindItem.getItems())
266 child.dispose();
267 }
268 else
269 kindItem.dispose();
270 }
271 }