001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
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.reflect.JavaType.Kind;
045 import org.granite.generator.template.StandardTemplateUris;
046
047 /**
048 * @author Franck WOLFF
049 */
050 public class TemplatesPanel extends Composite {
051
052 private final GraniteProperties properties;
053
054 private Tree templatesTree = null;
055 private boolean initialized = false;
056
057 public TemplatesPanel(Composite parent, GraniteBuilderContext context) throws CoreException {
058 super(parent, SWT.NONE);
059 this.properties = context.getProperties();
060 initializeComponents();
061 }
062
063 public Set<Gas3Template> getTemplates() {
064 if (!initialized)
065 return properties.getGas3().getTemplates();
066
067 Set<Gas3Template> templates = new HashSet<Gas3Template>(templatesTree.getItemCount());
068 for (TreeItem kindItem : templatesTree.getItems()) {
069 StringBuilder sb = new StringBuilder();
070 if (kindItem.getItemCount() > 0)
071 sb.append((String)kindItem.getItem(0).getData());
072 if (kindItem.getItemCount() > 1)
073 sb.append(';').append((String)kindItem.getItem(1).getData());
074 templates.add(new Gas3Template((Kind)kindItem.getData(), sb.toString()));
075 }
076 return templates;
077 }
078
079 @Override
080 public Rectangle getClientArea() {
081 initializeContent();
082 return super.getClientArea();
083 }
084
085 private void initializeContent() {
086 if (!initialized) {
087 for (Kind kind : Kind.values()) {
088 TreeItem kindItem = SWTUtil.addTreeItem(templatesTree, SWTUtil.IMG_TEMPLATE, kind.name(), null, null);
089 kindItem.setData(kind);
090 TemplateUri[] uris = properties.getGas3().getMatchingTemplateUris(kind);
091 for (TemplateUri uri : uris) {
092 TreeItem uriItem = SWTUtil.addTreeItem(kindItem, SWTUtil.IMG_FILE, uri.getUri() + (uri.isBase() ? " (base)" : ""), null, null);
093 uriItem.setData(uri.getUri());
094 }
095 kindItem.setExpanded(true);
096 }
097 initialized = true;
098 }
099 }
100
101 private void initializeComponents() {
102 setLayout(new GridLayout(2, false));
103
104 Label text = new Label(this, SWT.NONE);
105 text.setText("Templates used for generation:");
106 text.setLayoutData(SWTUtil.newGridData(SWT.NONE, 2));
107
108 templatesTree = new Tree(this, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
109 templatesTree.setLayoutData(new GridData(
110 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL |
111 GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL
112 ));
113
114
115 Composite buttons = new Composite(this, SWT.NONE);
116 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
117 buttons.setLayout(new FillLayout(SWT.VERTICAL));
118
119 final Button editButton = SWTUtil.newButton(buttons, "Edit...", false, new SelectionAdapter() {
120 @Override
121 public void widgetSelected(SelectionEvent e) {
122 editTemplatesHandler(e);
123 }
124 });
125
126 SWTUtil.newButton(buttons, "Reset to default", true, new SelectionAdapter() {
127 @Override
128 public void widgetSelected(SelectionEvent e) {
129 properties.getGas3().getTemplates().clear();
130 properties.getGas3().getTemplates().addAll(GraniteProperties.getDefaultProperties().getGas3().getTemplates());
131 for (TreeItem item : templatesTree.getItems())
132 item.dispose();
133 initialized = false;
134 initializeContent();
135 }
136 });
137
138 SWTUtil.newButton(buttons, "Use Tide", true, new SelectionAdapter() {
139 @Override
140 public void widgetSelected(SelectionEvent e) {
141 Gas3Template template = properties.getGas3().getTemplate(Kind.ENTITY);
142 template.setUri(StandardTemplateUris.TIDE_ENTITY_BASE, true);
143 template = properties.getGas3().getTemplate(Kind.REMOTE_DESTINATION);
144 template.setUri(StandardTemplateUris.TIDE_REMOTE_BASE, true);
145 for (TreeItem item : templatesTree.getItems())
146 item.dispose();
147 initialized = false;
148 initializeContent();
149 }
150 });
151
152 templatesTree.addSelectionListener(new SelectionAdapter() {
153 @Override
154 public void widgetSelected(SelectionEvent e) {
155 // Enable/Disable buttons based on selected tree item.
156 editButton.setEnabled(templatesTree.getSelection() != null && templatesTree.getSelection().length > 0);
157 }
158 });
159 }
160
161 private void editTemplatesHandler(SelectionEvent e) {
162 if (templatesTree.getSelection() == null || templatesTree.getSelection().length == 0)
163 return;
164
165 TreeItem kindItem = templatesTree.getSelection()[0];
166 while (kindItem.getParentItem() != null)
167 kindItem = kindItem.getParentItem();
168
169 String templateUri = (kindItem.getItemCount() <= 0 ? "" : (String)kindItem.getItems()[0].getData());
170 String baseTemplateUri = (kindItem.getItemCount() <= 1 ? "" : (String)kindItem.getItems()[1].getData());
171
172 String[] uris = Dialogs.editTemplateUris(
173 getDisplay().getActiveShell(),
174 "Templates for " + kindItem.getText() + " type",
175 templateUri,
176 baseTemplateUri
177 );
178
179 if (uris != null) {
180 if (kindItem.getItemCount() > 0) {
181 kindItem.getItem(0).setText(uris[0]);
182 kindItem.getItem(0).setData(uris[0]);
183 }
184 else {
185 TreeItem uriItem = SWTUtil.addTreeItem(kindItem, SWTUtil.IMG_FILE, uris[0], null, null);
186 uriItem.setData(uris[0]);
187 }
188
189 if (uris[1].length() > 0) {
190 if (kindItem.getItemCount() > 1) {
191 kindItem.getItem(1).setText(uris[1]);
192 kindItem.getItem(1).setData(uris[1]);
193 }
194 else {
195 TreeItem uriItem = SWTUtil.addTreeItem(kindItem, SWTUtil.IMG_FILE, uris[1], null, null);
196 uriItem.setData(uris[1]);
197 }
198 }
199 else if (kindItem.getItemCount() > 1)
200 kindItem.getItem(1).dispose();
201 }
202 }
203 }