ingo@1309: package de.intevation.flys.client.client.ui;
ingo@1309:
felix@1451: import java.util.Arrays;
raimund@1341: import java.util.LinkedHashMap;
raimund@1341:
ingo@1309: import com.google.gwt.core.client.GWT;
ingo@1309: import com.google.gwt.user.client.rpc.AsyncCallback;
ingo@1309:
felix@1499: import com.smartgwt.client.util.SC;
felix@1499:
ingo@1309: import com.smartgwt.client.widgets.Window;
ingo@1309: import com.smartgwt.client.widgets.layout.VLayout;
ingo@1309: import com.smartgwt.client.widgets.layout.HLayout;
ingo@1309: import com.smartgwt.client.widgets.Button;
ingo@1309: import com.smartgwt.client.widgets.form.DynamicForm;
ingo@1309: import com.smartgwt.client.widgets.form.fields.FormItem;
ingo@1309: import com.smartgwt.client.widgets.form.fields.CheckboxItem;
ingo@1309: import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
raimund@1341: import com.smartgwt.client.widgets.form.fields.SelectItem;
raimund@1337: import com.smartgwt.client.widgets.form.fields.StaticTextItem;
ingo@1309:
ingo@1309: import com.smartgwt.client.widgets.events.ClickEvent;
ingo@1309: import com.smartgwt.client.widgets.events.ClickHandler;
ingo@1309: import com.smartgwt.client.widgets.form.events.ItemChangedEvent;
ingo@1309: import com.smartgwt.client.widgets.form.events.ItemChangedHandler;
ingo@1309: import com.smartgwt.client.types.Alignment;
ingo@1309:
ingo@1309: import de.intevation.flys.client.shared.model.Collection;
ingo@1309: import de.intevation.flys.client.shared.model.CollectionItemAttribute;
ingo@1309: import de.intevation.flys.client.shared.model.Style;
ingo@1309: import de.intevation.flys.client.shared.model.StyleSetting;
ingo@1309: import de.intevation.flys.client.shared.model.FacetRecord;
raimund@1326: import de.intevation.flys.client.shared.model.Theme;
ingo@1309:
ingo@1309: import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync;
ingo@1309: import de.intevation.flys.client.client.services.CollectionItemAttributeService;
raimund@1336: import de.intevation.flys.client.client.ui.ThemePanel;
ingo@1309:
ingo@1309: import de.intevation.flys.client.client.FLYSConstants;
ingo@1309: import de.intevation.flys.client.client.Config;
ingo@1309:
ingo@1309: /**
ingo@1309: * @author Raimund Renkert
ingo@1309: */
ingo@1309: public class StyleEditorWindow
ingo@1309: extends Window
ingo@1309: implements ClickHandler
ingo@1309: {
ingo@1309: /** The interface that provides i18n messages. */
ingo@1309: protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
ingo@1309:
felix@1333: /** The collection. */
ingo@1309: protected Collection collection;
ingo@1309:
raimund@1336: /** The parent ThemePanel. */
raimund@1336: protected ThemePanel panel;
ingo@1309:
felix@1333: /** The attributes. */
ingo@1309: protected CollectionItemAttribute attributes;
ingo@1309:
ingo@1309: /** The selected facet. */
ingo@1309: protected FacetRecord facet;
ingo@1309:
felix@1333: /** Main layout. */
ingo@1309: protected VLayout layout;
ingo@1309:
ingo@1309: /** The service used to set collection item attributes. */
ingo@1309: protected CollectionItemAttributeServiceAsync itemAttributeService =
ingo@1309: GWT.create(CollectionItemAttributeService.class);
ingo@1309:
felix@1333:
raimund@1345: /**
raimund@1345: * Setup editor dialog.
raimund@1345: * @param collection The collection the current theme belongs to.
raimund@1345: * @param attributes The collection attributes.
raimund@1345: * @param facet The selected facet.
raimund@1345: */
ingo@1309: public StyleEditorWindow (
ingo@1309: Collection collection,
ingo@1309: CollectionItemAttribute attributes,
ingo@1309: FacetRecord facet)
ingo@1309: {
ingo@1309: this.collection = collection;
ingo@1309: this.attributes = attributes;
ingo@1309: this.facet = facet;
ingo@1309: this.layout = new VLayout();
ingo@1309:
ingo@1309: init();
ingo@1309: initPanels();
ingo@1309: }
ingo@1309:
ingo@1309:
raimund@1345: /**
raimund@1345: * Initialize the window and set the layout.
raimund@1345: */
ingo@1309: protected void init() {
ingo@1309: setTitle(MSG.properties());
ingo@1309: setCanDragReposition(true);
ingo@1309: setCanDragResize(true);
raimund@1381: layout.setMargin(10);
ingo@1309:
ingo@1309: layout.setWidth100();
ingo@1309: layout.setHeight100();
ingo@1309: }
ingo@1309:
ingo@1309:
raimund@1345: /**
raimund@1345: * Initialize the static window content like buttons and main layout.
raimund@1345: */
ingo@1309: protected void initPanels() {
ingo@1309: HLayout buttons = new HLayout();
ingo@1309: Button accept = new Button(MSG.label_ok());
ingo@1309: Button cancel = new Button(MSG.label_cancel());
ingo@1309: cancel.addClickHandler(this);
ingo@1309: accept.addClickHandler(new ClickHandler() {
ingo@1309: public void onClick(ClickEvent e) {
ingo@1309: saveStyle();
ingo@1309: }
ingo@1309: });
ingo@1309:
ingo@1309: buttons.addMember(accept);
ingo@1309: buttons.addMember(cancel);
raimund@1381: buttons.setAlign(Alignment.CENTER);
raimund@1381: buttons.setHeight(30);
ingo@1309:
raimund@1348: VLayout propGrid = createPropertyGrid();
raimund@1348:
raimund@1348: layout.addMember(propGrid);
ingo@1309: layout.addMember(buttons);
ingo@1309: addItem(layout);
ingo@2472: setWidth(400);
raimund@1381: setHeight(410);
ingo@1309: }
ingo@1309:
ingo@1309:
raimund@1345: /**
raimund@1345: * Setter for the parent panel.
raimund@1345: * @param panel The panel.
raimund@1345: */
raimund@1336: public void setThemePanel (ThemePanel panel) {
raimund@1336: this.panel = panel;
ingo@1309: }
ingo@1309:
ingo@1309:
raimund@1345: /**
raimund@1345: * this method is called when the user aborts theming.
raimund@1345: * @param event The event.
raimund@1345: */
ingo@1309: public void onClick(ClickEvent event) {
ingo@1309: this.hide();
ingo@1309: }
ingo@1309:
ingo@1309:
raimund@1345: /**
raimund@1345: * This method creates the property grid for available styling attributes.
raimund@1345: * @return The layout containing the UI elements.
raimund@1345: */
ingo@1309: protected VLayout createPropertyGrid() {
ingo@1309: VLayout properties = new VLayout();
ingo@1309:
felix@1450: Theme theme = facet.getTheme();
felix@1450: Style style = attributes.getStyle(theme.getFacet(), theme.getIndex());
ingo@1309:
raimund@1337: StaticTextItem name = new StaticTextItem("name", "Name");
ingo@1309: name.setValue(facet.getName());
ingo@2472: name.setTitleStyle("color:#000;");
ingo@1309: name.setTitleAlign(Alignment.LEFT);
ingo@1309: name.setDisabled(true);
ingo@1309: name.setShowDisabled(false);
felix@1450: DynamicForm form = new DynamicForm();
felix@1450: form.setFields(name);
ingo@2472: form.setColWidths("40%", "60%");
ingo@2472:
felix@1450: properties.addMember(form);
ingo@1309:
felix@1499: if (style == null) {
felix@1499: SC.warn("No style for " + theme.getFacet() + " found.");
felix@1499: return properties;
felix@1499: }
felix@1499:
felix@1451: // Done via array to keep the order.
felix@1451: String[] sets = {"showlines",
felix@1451: "showpoints",
felix@1451: "linetype",
felix@1451: "linesize",
felix@1451: "linecolor",
felix@1451: "font",
felix@1451: "textstyle",
felix@1451: "textsize",
ingo@1495: "pointsize",
felix@1451: "textcolor",
felix@1451: "textorientation",
felix@1451: "backgroundcolor",
felix@2499: "showbackground",
felix@2499: "showlinelabel",
felix@2499: "linelabelfont",
felix@2499: "linelabelcolor",
felix@2499: "linelabelsize",
felix@2499: "linelabelstyle",
felix@2499: "linelabelshowbg",
felix@2499: "linelabelbgcolor"};
felix@1451:
felix@1451: for (String settingName: sets) {
felix@1451: StyleSetting set = style.getSetting(settingName);
felix@1451:
felix@1451: if (set == null) {
felix@1451: continue;
felix@1451: }
felix@1451:
felix@1451: DynamicForm property = createPropertyUI(
felix@1451: set.getDisplayName(),
felix@1451: set.getName(),
felix@1451: set.getType(),
felix@1451: set.getDefaultValue());
felix@1451: properties.addMember(property);
felix@1451: }
felix@1451:
felix@1451: // Add settings not in whitelist above.
felix@1450: for (StyleSetting set: style.getSettings()) {
raimund@1380:
felix@1451: if (Arrays.asList(sets).contains(set.getName()) || set == null) {
raimund@1380: continue;
raimund@1380: }
raimund@1380:
ingo@1309: DynamicForm property = createPropertyUI(
ingo@1309: set.getDisplayName(),
ingo@1309: set.getName(),
ingo@1309: set.getType(),
ingo@1309: set.getDefaultValue());
ingo@1309: properties.addMember(property);
ingo@1309: }
felix@1450:
ingo@1309: return properties;
ingo@1309: }
ingo@1309:
raimund@1336:
raimund@1345: /**
raimund@1345: * Create a property form.
raimund@1345: * @param dname The display name.
raimund@1345: * @param name The property name.
raimund@1345: * @param type The property type.
raimund@1345: * @param value The current value.
raimund@1345: *
raimund@1345: * @return The dynamic form for the attribute property.
raimund@1345: */
ingo@1309: protected DynamicForm createPropertyUI(
ingo@1309: String dname,
ingo@1309: String name,
ingo@1309: String type,
ingo@1309: String value)
ingo@1309: {
ingo@1309: DynamicForm df = new DynamicForm();
ingo@2472: df.setColWidths("40%", "60%");
ingo@1309:
ingo@1309: FormItem f;
ingo@1309: if(type.equals("int")) {
raimund@1479: f = new SelectItem(name, MSG.getString(name));
raimund@1348: if (name.equals("linesize")) {
raimund@1348: f = createLineSizeUI(f);
raimund@1348: }
felix@2499: else if (name.equals("textsize") || name.equals("linelabelsize")) {
raimund@1348: LinkedHashMap valueMap = new LinkedHashMap();
raimund@1348: valueMap.put("3", "3");
raimund@1348: valueMap.put("5", "5");
raimund@1348: valueMap.put("8", "8");
raimund@1348: valueMap.put("10", "10");
raimund@1348: valueMap.put("12", "12");
raimund@1348: valueMap.put("14", "14");
raimund@1348: valueMap.put("18", "18");
raimund@1348: valueMap.put("24", "24");
raimund@1348: f.setValueMap(valueMap);
raimund@1348: }
ingo@1495: else if (name.equals("pointsize")) {
ingo@1495: LinkedHashMap valueMap = new LinkedHashMap();
ingo@1495: valueMap.put("1", "1");
felix@2433: valueMap.put("2", "2");
ingo@1495: valueMap.put("3", "3");
felix@2433: valueMap.put("4", "4");
ingo@1495: valueMap.put("5", "5");
felix@2433: valueMap.put("6", "6");
ingo@1495: valueMap.put("7", "7");
ingo@1495: f.setValueMap(valueMap);
ingo@1495: }
raimund@1341: f.setValue(value);
ingo@1309: }
ingo@1309: else if (type.equals("boolean")) {
raimund@1359: if(name.equals("textorientation")) {
raimund@1479: f = new SelectItem(name, MSG.getString(name));
raimund@1359: LinkedHashMap valueMap = new LinkedHashMap();
raimund@1359: valueMap.put("true", "horizontal");
raimund@1359: valueMap.put("false", "vertical");
raimund@1359: f.setValueMap(valueMap);
raimund@1359: f.setValue(value);
ingo@1309: }
ingo@1309: else {
raimund@1479: CheckboxItem c = new CheckboxItem(name, MSG.getString(name));
raimund@1359: if(value.equals("true")) {
raimund@1359: c.setValue(true);
raimund@1359: }
raimund@1359: else {
raimund@1359: c.setValue(false);
raimund@1359: }
raimund@1359: c.setLabelAsTitle(true);
raimund@1359: f = c;
ingo@1309: }
ingo@1309: }
ingo@1309: else if (type.equals("Color")) {
raimund@1479: ColorPickerItem c = new ColorPickerItem(name, MSG.getString(name));
ingo@1309: c.setValue(rgbToHtml(value));
ingo@1309: f = c;
ingo@1309: }
ingo@1309: else if (type.equals("Dash")) {
raimund@1479: f = new SelectItem(name, MSG.getString(name));
raimund@1341: LinkedHashMap valueIcons = new LinkedHashMap();
raimund@1341: f.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/linestyle-dash-");
raimund@1341: f.setImageURLSuffix(".png");
raimund@1341: f.setValueIconHeight(20);
raimund@1341: f.setValueIconWidth(80);
raimund@1348: LinkedHashMap valueMap = new LinkedHashMap();
raimund@1341: valueMap.put("10", "");
raimund@1341: valueMap.put("10,5", "");
raimund@1341: valueMap.put("20,10", "");
raimund@1341: valueMap.put("30,10", "");
raimund@1341: valueMap.put("20,5,15,5", "");
raimund@1341: valueIcons.put("10", "10");
raimund@1341: valueIcons.put("10,5", "10-5");
raimund@1341: valueIcons.put("20,10", "20-10");
raimund@1341: valueIcons.put("30,10", "30-10");
raimund@1341: valueIcons.put("20,5,15,5", "20-5-15-5");
raimund@1341: f.setValueIcons(valueIcons);
raimund@1341: f.setValueMap(valueMap);
ingo@1309: f.setValue(value);
ingo@1309: }
raimund@1348: else if (type.equals("Font")) {
raimund@1479: f = new SelectItem(name, MSG.getString(name));
raimund@1348: LinkedHashMap valueMap = new LinkedHashMap();
raimund@1348: valueMap.put("arial", "Arial");
raimund@1348: valueMap.put("courier", "Courier");
raimund@1348: valueMap.put("verdana", "Verdana");
raimund@1348: valueMap.put("times", "Times");
raimund@1348: f.setValueMap(valueMap);
raimund@1348: f.setValue(value);
raimund@1348: }
raimund@1359: else if (type.equals("Style")) {
raimund@1479: f = new SelectItem(name, MSG.getString(name));
raimund@1359: LinkedHashMap valueMap = new LinkedHashMap();
raimund@1359: valueMap.put("standard", "Normal");
raimund@1359: valueMap.put("bold", "Bold");
raimund@1359: valueMap.put("italic", "Italic");
raimund@1359: f.setValueMap(valueMap);
raimund@1359: f.setValue(value);
raimund@1359: }
raimund@2458: else if (type.equals("Symbol")) {
raimund@2458: //create an empty element as long as this property can not be
raimund@2458: //changed.
raimund@2458: f = new StaticTextItem("");
raimund@2458: }
ingo@1309: else {
ingo@1309: f = new FormItem();
ingo@1309: }
ingo@2472: f.setTitleStyle("color:#000;");
ingo@1309: f.setTitleAlign(Alignment.LEFT);
ingo@1309: df.setFields(f);
ingo@1309: df.addItemChangedHandler(new ItemChangedHandler() {
ingo@1309: public void onItemChanged(ItemChangedEvent e) {
ingo@1309: String name = e.getItem().getName();
ingo@1309: String newValue = e.getNewValue().toString();
ingo@1309: setNewValue(name, newValue);
ingo@1309: }
ingo@1309: });
ingo@1309:
ingo@1309: return df;
ingo@1309: }
ingo@1309:
felix@1333:
raimund@1348: protected FormItem createLineSizeUI(FormItem f) {
raimund@1348: LinkedHashMap valueIcons = new LinkedHashMap();
raimund@1348: f.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/linestyle-");
raimund@1348: f.setImageURLSuffix("px.png");
raimund@1348: f.setValueIconHeight(20);
raimund@1348: f.setValueIconWidth(80);
raimund@1348: LinkedHashMap valueMap = new LinkedHashMap();
raimund@1348: valueMap.put("1", "");
raimund@1348: valueMap.put("2", "");
raimund@1348: valueMap.put("3", "");
raimund@1348: valueMap.put("4", "");
raimund@1348: valueMap.put("5", "");
raimund@1348: valueMap.put("6", "");
raimund@1348: valueMap.put("7", "");
raimund@1348: valueMap.put("8", "");
raimund@1348: valueIcons.put("1", "1");
raimund@1348: valueIcons.put("2", "2");
raimund@1348: valueIcons.put("3", "3");
raimund@1348: valueIcons.put("4", "4");
raimund@1348: valueIcons.put("5", "5");
raimund@1348: valueIcons.put("6", "6");
raimund@1348: valueIcons.put("7", "7");
raimund@1348: valueIcons.put("8", "8");
raimund@1348: f.setValueIcons(valueIcons);
raimund@1348: f.setValueMap(valueMap);
raimund@1348: return f;
raimund@1348: }
raimund@1348:
raimund@1348:
raimund@1345: /**
raimund@1345: * Static method to convert a color from RGB to HTML notation.
raimund@1345: * @param rgb String in RGB notation.
raimund@1345: *
raimund@1345: * @return String in HTML notation.
raimund@1345: */
felix@1333: protected static String rgbToHtml(String rgb) {
ingo@1309: String[] parts = rgb.split(",");
ingo@1309: int values[] = new int[parts.length];
ingo@1309: for (int i = 0; i < parts.length; i++) {
ingo@1309: parts[i] = parts[i].trim();
ingo@1309: try {
ingo@1309: values[i] = Integer.parseInt(parts[i]);
ingo@1309: }
ingo@1309: catch(NumberFormatException nfe) {
ingo@1309: return "#000000";
ingo@1309: }
ingo@1309: }
ingo@1309: String hex = "#";
ingo@1309: for (int i = 0; i < values.length; i++) {
ingo@1309: if (values[i] < 16) {
ingo@1309: hex += "0";
ingo@1309: }
ingo@1309: hex += Integer.toHexString(values[i]);
ingo@1309: }
ingo@1309: return hex;
ingo@1309: }
ingo@1309:
felix@1333:
raimund@1345: /**
raimund@1345: * Static method to convert a color from HTML to RGB notation.
raimund@1345: * @param html String in HTML notation.
raimund@1345: *
raimund@1345: * @return String in RGB notation.
raimund@1345: */
felix@1333: protected static String htmlToRgb(String html) {
ingo@1309: if (!html.startsWith("#")) {
ingo@1309: return "0, 0, 0";
ingo@1309: }
ingo@1309:
ingo@1309: int r = Integer.valueOf(html.substring(1, 3), 16);
ingo@1309: int g = Integer.valueOf(html.substring(3, 5), 16);
ingo@1309: int b = Integer.valueOf(html.substring(5, 7), 16);
ingo@1309:
ingo@1309: return r + ", " + g + ", " + b;
ingo@1309: }
ingo@1309:
felix@1333:
raimund@1345: /**
raimund@1345: * Saves the current style attributes and requests a redraw.
raimund@1345: */
ingo@1309: protected void saveStyle () {
ingo@1309: GWT.log("StyleEditorWindow.saveStyle()");
ingo@1309: Config config = Config.getInstance();
ingo@1309: String locale = config.getLocale();
ingo@1309:
ingo@1309: itemAttributeService.setCollectionItemAttribute(
ingo@1309: this.collection,
ingo@1309: attributes.getArtifact(),
ingo@1309: locale,
ingo@1309: attributes,
ingo@1309: new AsyncCallback() {
ingo@1309: public void onFailure (Throwable caught) {
ingo@1309: GWT.log("Could not set Collection item attributes.");
ingo@1309: }
ingo@1309: public void onSuccess(Void v) {
ingo@1309: GWT.log("Successfully saved collection item attributes.");
raimund@1336: panel.requestRedraw();
ingo@1309: }
ingo@1309: });
ingo@1309:
ingo@1309:
ingo@1309: this.hide();
ingo@1309: }
ingo@1309:
felix@1333:
raimund@1345: /**
raimund@1345: * Sets a new value for an attribute.
raimund@1345: * @param name Attribute name.
raimund@1345: * @param value The new value.
raimund@1345: */
ingo@1309: protected final void setNewValue(String name, String value) {
raimund@1326: Theme t = facet.getTheme();
raimund@1326: Style s = attributes.getStyle(t.getFacet(), t.getIndex());
ingo@1309: StyleSetting set = s.getSetting(name);
ingo@2472: String type = set.getType();
ingo@2472:
ingo@2472: if(name.indexOf("color") != -1
ingo@2472: || (type != null && type.toLowerCase().indexOf("color") > -1)) {
ingo@1309: value = htmlToRgb(value);
ingo@1309: }
ingo@1309: set.setDefaultValue(value);
ingo@1309: }
ingo@1309: }
ingo@1309: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :