# HG changeset patch # User Ingo Weinzierl # Date 1318409597 0 # Node ID a95e82d6bcc12863daeda64a8855d5ab4eb99179 # Parent d194bee456d361391b00bdb467a19d491ba1ab25 Refactored the code to create a context menu and a style editor so that it is also available for maps. flys-client/trunk@2943 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/ChangeLog --- a/flys-client/ChangeLog Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/ChangeLog Wed Oct 12 08:53:17 2011 +0000 @@ -1,3 +1,22 @@ +2011-10-12 Ingo Weinzierl + + * src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java, + src/main/java/de/intevation/flys/client/client/ui/ThemePanel.java: Moved + the ContextMenu creation from ChartThemePanel to ThemePanel - we want a + ContextMenu in the ChartThemePanel as well. In addition, there are new + menu items to activate/deactivate and remove themes. + + * src/main/java/de/intevation/flys/client/client/ui/chart/StyleEditorWindow.java, + src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java: + Moved the StyleEditorWindow to an upper package, because it will also be + used in the map. + + * src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties, + src/main/java/de/intevation/flys/client/client/FLYSConstants.java, + src/main/java/de/intevation/flys/client/client/FLYSConstants.properties, + src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties: + Added new strings for context menu items of the ThemePanel. + 2011-10-12 Felix Wolfsteller Show only "W"s in input helper for W-Diffs. diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java Wed Oct 12 08:53:17 2011 +0000 @@ -224,6 +224,12 @@ String properties(); + String activateTheme(); + + String deactivateTheme(); + + String removeTheme(); + String label_ok(); String label_cancel(); diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties --- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties Wed Oct 12 08:53:17 2011 +0000 @@ -49,6 +49,9 @@ search = Search discharge = FIXME(Name) properties = Properties +activateTheme = Activate +deactivateTheme = Deactivate +removeTheme = Remove label_ok = Ok label_cancel = Cancel diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties --- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties Wed Oct 12 08:53:17 2011 +0000 @@ -49,6 +49,9 @@ search = Suchbegriff discharge = Kennzeichnender Abfluss properties = Eigenschaften +activateTheme = Aktivieren +deactivateTheme = Deaktivieren +removeTheme = Entfernen label_ok = Ok label_cancel = Abbrechen diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties --- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties Wed Oct 12 08:53:17 2011 +0000 @@ -49,6 +49,9 @@ search = Search discharge = FIXME(Name) properties = Properties +activateTheme = Activate +deactivateTheme = Deactivate +removeTheme = Remove label_ok = Ok label_cancel = Cancel diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java Wed Oct 12 08:53:17 2011 +0000 @@ -0,0 +1,276 @@ +package de.intevation.flys.client.client.ui; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.user.client.rpc.AsyncCallback; + +import com.smartgwt.client.widgets.Window; +import com.smartgwt.client.widgets.layout.VLayout; +import com.smartgwt.client.widgets.layout.HLayout; +import com.smartgwt.client.widgets.Button; +import com.smartgwt.client.widgets.form.DynamicForm; +import com.smartgwt.client.widgets.form.fields.FormItem; +import com.smartgwt.client.widgets.form.fields.CheckboxItem; +import com.smartgwt.client.widgets.form.fields.SpinnerItem; +import com.smartgwt.client.widgets.form.fields.ColorPickerItem; +import com.smartgwt.client.widgets.form.fields.ComboBoxItem; +import com.smartgwt.client.widgets.form.fields.TextItem; + +import com.smartgwt.client.widgets.events.ClickEvent; +import com.smartgwt.client.widgets.events.ClickHandler; +import com.smartgwt.client.widgets.form.events.ItemChangedEvent; +import com.smartgwt.client.widgets.form.events.ItemChangedHandler; +import com.smartgwt.client.types.Alignment; + +import de.intevation.flys.client.shared.model.Collection; +import de.intevation.flys.client.shared.model.CollectionItemAttribute; +import de.intevation.flys.client.shared.model.Style; +import de.intevation.flys.client.shared.model.StyleSetting; +import de.intevation.flys.client.shared.model.FacetRecord; + +import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync; +import de.intevation.flys.client.client.services.CollectionItemAttributeService; +import de.intevation.flys.client.client.ui.CollectionView; + +import de.intevation.flys.client.client.FLYSConstants; +import de.intevation.flys.client.client.Config; + +/** + * @author Raimund Renkert + */ +public class StyleEditorWindow +extends Window +implements ClickHandler +{ + /** The interface that provides i18n messages. */ + protected FLYSConstants MSG = GWT.create(FLYSConstants.class); + + /** The collection */ + protected Collection collection; + + /** The parent collection view */ + protected CollectionView view; + + /** The attributes */ + protected CollectionItemAttribute attributes; + + /** The selected facet. */ + protected FacetRecord facet; + + /** Main layout */ + protected VLayout layout; + + /** The service used to set collection item attributes. */ + protected CollectionItemAttributeServiceAsync itemAttributeService = + GWT.create(CollectionItemAttributeService.class); + + public StyleEditorWindow ( + Collection collection, + CollectionItemAttribute attributes, + FacetRecord facet) + { + this.collection = collection; + this.attributes = attributes; + this.facet = facet; + this.layout = new VLayout(); + + init(); + initPanels(); + } + + + protected void init() { + setTitle(MSG.properties()); + setWidth(270); + setHeight(200); + setCanDragReposition(true); + setCanDragResize(true); + setKeepInParentRect(true); + + layout.setWidth100(); + layout.setHeight100(); + + } + + + protected void initPanels() { + HLayout buttons = new HLayout(); + Button accept = new Button(MSG.label_ok()); + Button cancel = new Button(MSG.label_cancel()); + cancel.addClickHandler(this); + accept.addClickHandler(new ClickHandler() { + public void onClick(ClickEvent e) { + saveStyle(); + } + }); + + buttons.addMember(accept); + buttons.addMember(cancel); + buttons.setAlign(Alignment.RIGHT); + + layout.addMember(createPropertyGrid());; + layout.addMember(buttons); + addItem(layout); + } + + + public void setCollectionView (CollectionView view) { + this.view = view; + setParentElement(this.view.getParentElement()); + } + + + public void onClick(ClickEvent event) { + this.hide(); + } + + + protected VLayout createPropertyGrid() { + VLayout properties = new VLayout(); + + Style s = attributes.getStyle(facet.getTheme().getFacet()); + + TextItem name = new TextItem("name", "Name"); + name.setValue(facet.getName()); + name.setTitleStyle("color:#000; width:120px"); + name.setTitleAlign(Alignment.LEFT); + name.setDisabled(true); + name.setShowDisabled(false); + DynamicForm f = new DynamicForm(); + f.setFields(name); + properties.addMember(f); + + for (int i = 0; i < s.getNumSettings(); i ++) { + final StyleSetting set = s.getSetting(i); + DynamicForm property = createPropertyUI( + set.getDisplayName(), + set.getName(), + set.getType(), + set.getDefaultValue()); + properties.addMember(property); + } + return properties; + } + + protected DynamicForm createPropertyUI( + String dname, + String name, + String type, + String value) + { + DynamicForm df = new DynamicForm(); + + FormItem f; + if(type.equals("int")) { + SpinnerItem s = new SpinnerItem(name, dname); + s.setMin(1); + s.setMax(10); + s.setValue(value); + f = s; + } + else if (type.equals("boolean")) { + CheckboxItem c = new CheckboxItem(name, dname); + if(value.equals("true")) { + c.setValue(true); + } + else { + c.setValue(false); + } + c.setLabelAsTitle(true); + f = c; + } + else if (type.equals("Color")) { + ColorPickerItem c = new ColorPickerItem(name, dname); + c.setValue(rgbToHtml(value)); + f = c; + } + else if (type.equals("Dash")) { + f = new ComboBoxItem(name, dname); + f.setValue(value); + } + else { + f = new FormItem(); + } + f.setTitleStyle("color:#000; width:120px"); + f.setTitleAlign(Alignment.LEFT); + df.setFields(f); + df.addItemChangedHandler(new ItemChangedHandler() { + public void onItemChanged(ItemChangedEvent e) { + String name = e.getItem().getName(); + String newValue = e.getNewValue().toString(); + GWT.log("changed: " + name); + setNewValue(name, newValue); + } + }); + + return df; + } + + protected String rgbToHtml(String rgb) { + String[] parts = rgb.split(","); + int values[] = new int[parts.length]; + for (int i = 0; i < parts.length; i++) { + parts[i] = parts[i].trim(); + try { + values[i] = Integer.parseInt(parts[i]); + } + catch(NumberFormatException nfe) { + return "#000000"; + } + } + String hex = "#"; + for (int i = 0; i < values.length; i++) { + if (values[i] < 16) { + hex += "0"; + } + hex += Integer.toHexString(values[i]); + } + return hex; + } + + protected String htmlToRgb(String html) { + if (!html.startsWith("#")) { + return "0, 0, 0"; + } + + int r = Integer.valueOf(html.substring(1, 3), 16); + int g = Integer.valueOf(html.substring(3, 5), 16); + int b = Integer.valueOf(html.substring(5, 7), 16); + + return r + ", " + g + ", " + b; + } + + protected void saveStyle () { + GWT.log("StyleEditorWindow.saveStyle()"); + Config config = Config.getInstance(); + String url = config.getServerUrl(); + String locale = config.getLocale(); + + itemAttributeService.setCollectionItemAttribute( + this.collection, + attributes.getArtifact(), + url, + locale, + attributes, + new AsyncCallback() { + public void onFailure (Throwable caught) { + GWT.log("Could not set Collection item attributes."); + } + public void onSuccess(Void v) { + GWT.log("Successfully saved collection item attributes."); + } + }); + + + this.hide(); + } + + protected final void setNewValue(String name, String value) { + Style s = attributes.getStyle(facet.getTheme().getFacet()); + StyleSetting set = s.getSetting(name); + if(name.equals("linecolor")) { + value = htmlToRgb(value); + } + set.setDefaultValue(value); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/ui/ThemePanel.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/ThemePanel.java Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/ThemePanel.java Wed Oct 12 08:53:17 2011 +0000 @@ -12,8 +12,15 @@ import com.smartgwt.client.widgets.grid.ListGridRecord; import com.smartgwt.client.widgets.grid.events.EditCompleteEvent; import com.smartgwt.client.widgets.grid.events.EditCompleteHandler; +import com.smartgwt.client.widgets.grid.events.RowContextClickEvent; +import com.smartgwt.client.widgets.grid.events.RowContextClickHandler; +import com.smartgwt.client.widgets.menu.Menu; +import com.smartgwt.client.widgets.menu.MenuItem; +import com.smartgwt.client.widgets.menu.events.ClickHandler; +import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent; import de.intevation.flys.client.shared.model.Collection; +import de.intevation.flys.client.shared.model.CollectionItemAttribute; import de.intevation.flys.client.shared.model.FacetRecord; import de.intevation.flys.client.shared.model.OutputMode; import de.intevation.flys.client.shared.model.Theme; @@ -32,6 +39,8 @@ import de.intevation.flys.client.client.event.RedrawRequestEvent.Type; import de.intevation.flys.client.client.services.CollectionAttributeService; import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync; +import de.intevation.flys.client.client.services.CollectionItemAttributeService; +import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync; public abstract class ThemePanel @@ -44,6 +53,10 @@ protected CollectionAttributeServiceAsync updater = GWT.create(CollectionAttributeService.class); + /** The service used to get collection item attributes. */ + protected CollectionItemAttributeServiceAsync itemAttributeService = + GWT.create(CollectionItemAttributeService.class); + private FLYSConstants MSG = GWT.create(FLYSConstants.class); /** List of OutParameterChangedHandler. */ @@ -57,6 +70,9 @@ protected ThemeNavigationPanel navigation; protected ListGrid list; + /** The collection view*/ + protected CollectionView view; + /** * Setup Grid, navigation bar. @@ -147,6 +163,11 @@ } + public void setCollectionView(CollectionView view) { + this.view = view; + } + + /** * This method is used to clear the current theme grid and add new updated * data. @@ -217,7 +238,33 @@ * Create and configure the Grid to display. */ protected ListGrid createGrid() { - return new ListGrid(); + ListGrid grid = new ListGrid(); + grid.addRowContextClickHandler(new RowContextClickHandler() { + public void onRowContextClick(RowContextClickEvent event) { + ListGridRecord[] records = list.getSelection(); + + Menu menu = null; + + if (records == null || records.length == 0) { + return; + } + else if (records.length == 1) { + menu = getSingleContextMenu(records); + } + else if (records.length > 1) { + menu = getMultiContextMenu(records); + } + + if (menu != null) { + list.setContextMenu(menu); + menu.showContextMenu(); + + event.cancel(); + } + } + }); + + return grid; } @@ -237,6 +284,98 @@ } + protected Menu getSingleContextMenu(final ListGridRecord[] records) { + Menu menu = new Menu(); + + menu.addItem(createActivateItem(records)); + menu.addItem(createDeactivateItem(records)); + menu.addItem(createRemoveItem(records)); + menu.addItem(createPropertiesItem(records)); + + return menu; + } + + + protected Menu getMultiContextMenu(final ListGridRecord[] records) { + Menu menu = new Menu(); + + menu.addItem(createActivateItem(records)); + menu.addItem(createDeactivateItem(records)); + menu.addItem(createRemoveItem(records)); + + return menu; + } + + + protected MenuItem createPropertiesItem(final ListGridRecord[] records) { + MenuItem properties = new MenuItem(MSG.properties()); + + properties.addClickHandler(new ClickHandler() { + public void onClick(MenuItemClickEvent evt) { + GWT.log("clicked properties"); + for (ListGridRecord record: records) { + openStyleEditor((FacetRecord) record); + } + } + }); + + return properties; + } + + + protected MenuItem createActivateItem(final ListGridRecord[] records) { + MenuItem activate = new MenuItem(MSG.activateTheme()); + + activate.addClickHandler(new ClickHandler() { + public void onClick(MenuItemClickEvent evt) { + for (ListGridRecord record: records) { + FacetRecord facet = (FacetRecord) record; + activateTheme(facet.getTheme(), true); + } + + updateCollection(); + } + }); + + return activate; + } + + + protected MenuItem createDeactivateItem(final ListGridRecord[] records) { + MenuItem deactivate = new MenuItem(MSG.deactivateTheme()); + + deactivate.addClickHandler(new ClickHandler() { + public void onClick(MenuItemClickEvent evt) { + for (ListGridRecord record: records) { + FacetRecord facet = (FacetRecord) record; + activateTheme(facet.getTheme(), false); + } + + updateCollection(); + } + }); + + return deactivate; + } + + + protected MenuItem createRemoveItem(final ListGridRecord[] records) { + MenuItem remove = new MenuItem(MSG.removeTheme()); + + remove.addClickHandler(new ClickHandler() { + public void onClick(MenuItemClickEvent evt) { + SC.warn("Currently not implemented"); + + for (ListGridRecord record: records) { + FacetRecord facet = (FacetRecord) record; + } + } + }); + + return remove; + } + + /** * This method is called after a cell in the theme grid has been modified. * @@ -380,5 +519,34 @@ updateGrid(); } + + + protected void openStyleEditor(final FacetRecord record) { + Config config = Config.getInstance(); + String url = config.getServerUrl(); + String locale = config.getLocale(); + + String artifact = record.getTheme().getArtifact(); + + itemAttributeService.getCollectionItemAttribute( + this.collection, + artifact, + url, + locale, + new AsyncCallback() { + public void onFailure (Throwable caught) { + GWT.log("Could not get Collection item attributes."); + } + public void onSuccess(CollectionItemAttribute cia) { + GWT.log("Successfully loaded collectionitem attributes."); + StyleEditorWindow win = new StyleEditorWindow( + collection, + cia, + record); + win.setCollectionView(view); + win.show(); + } + }); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java Wed Oct 12 07:41:04 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java Wed Oct 12 08:53:17 2011 +0000 @@ -1,33 +1,20 @@ package de.intevation.flys.client.client.ui.chart; import com.google.gwt.core.client.GWT; -import com.google.gwt.user.client.rpc.AsyncCallback; import com.smartgwt.client.types.ListGridFieldType; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.layout.VLayout; -import com.smartgwt.client.widgets.grid.events.RowContextClickEvent; -import com.smartgwt.client.widgets.grid.events.RowContextClickHandler; -import com.smartgwt.client.widgets.menu.Menu; -import com.smartgwt.client.widgets.menu.MenuItem; -import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent; -import com.smartgwt.client.widgets.menu.events.ClickHandler; import de.intevation.flys.client.shared.model.Collection; import de.intevation.flys.client.shared.model.Theme; import de.intevation.flys.client.shared.model.OutputMode; -import de.intevation.flys.client.shared.model.FacetRecord; -import de.intevation.flys.client.shared.model.CollectionItemAttribute; import de.intevation.flys.client.client.FLYSConstants; import de.intevation.flys.client.client.ui.ThemePanel; -import de.intevation.flys.client.client.Config; -import de.intevation.flys.client.client.ui.CollectionView; import de.intevation.flys.client.client.services.FeedService; import de.intevation.flys.client.client.services.FeedServiceAsync; -import de.intevation.flys.client.client.services.CollectionItemAttributeService; -import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync; /** * @author Ingo Weinzierl @@ -40,13 +27,6 @@ /** The collection */ protected Collection collection; - /** The collection view*/ - protected CollectionView view; - - /** The service used to get collection item attributes. */ - protected CollectionItemAttributeServiceAsync itemAttributeService = - GWT.create(CollectionItemAttributeService.class); - public static final String GRID_FIELD_ACTIVE = "active"; public static final String GRID_FIELD_NAME = "name"; public static final String GRID_FIELD_ACTIONS = "actions"; @@ -97,17 +77,6 @@ list.setHeight100(); list.addEditCompleteHandler(this); - list.addRowContextClickHandler(new RowContextClickHandler() { - public void onRowContextClick(RowContextClickEvent event) { - FacetRecord record = (FacetRecord) event.getRecord(); - - Menu menu = createContextMenu(record); - list.setContextMenu(menu); - menu.showContextMenu(); - - event.cancel(); - } - }); ListGridField active = new ListGridField(GRID_FIELD_ACTIVE, " ", 20); active.setType(ListGridFieldType.BOOLEAN); @@ -120,58 +89,9 @@ } - protected Menu createContextMenu(final FacetRecord record) { - Menu menu = new Menu(); - - MenuItem properties = new MenuItem(MSG.properties()); - properties.addClickHandler(new ClickHandler() { - public void onClick(MenuItemClickEvent evt) { - GWT.log("clicked properties"); - getItemAttributes(record); - } - }); - menu.addItem(properties); - return menu; - } - - @Override public void activateTheme(Theme theme, boolean active) { theme.setActive(active ? 1 : 0); } - - - protected void getItemAttributes(final FacetRecord record) { - Config config = Config.getInstance(); - String url = config.getServerUrl(); - String locale = config.getLocale(); - - String artifact = record.getTheme().getArtifact(); - - itemAttributeService.getCollectionItemAttribute( - this.collection, - artifact, - url, - locale, - new AsyncCallback() { - public void onFailure (Throwable caught) { - GWT.log("Could not get Collection item attributes."); - } - public void onSuccess(CollectionItemAttribute cia) { - GWT.log("Successfully loaded collectionitem attributes."); - StyleEditorWindow win = new StyleEditorWindow( - collection, - cia, - record); - win.setCollectionView(view); - win.show(); - } - }); - } - - - public void setCollectionView (CollectionView view) { - this.view = view; - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r d194bee456d3 -r a95e82d6bcc1 flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/StyleEditorWindow.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/StyleEditorWindow.java Wed Oct 12 07:41:04 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,276 +0,0 @@ -package de.intevation.flys.client.client.ui.chart; - -import com.google.gwt.core.client.GWT; -import com.google.gwt.user.client.rpc.AsyncCallback; - -import com.smartgwt.client.widgets.Window; -import com.smartgwt.client.widgets.layout.VLayout; -import com.smartgwt.client.widgets.layout.HLayout; -import com.smartgwt.client.widgets.Button; -import com.smartgwt.client.widgets.form.DynamicForm; -import com.smartgwt.client.widgets.form.fields.FormItem; -import com.smartgwt.client.widgets.form.fields.CheckboxItem; -import com.smartgwt.client.widgets.form.fields.SpinnerItem; -import com.smartgwt.client.widgets.form.fields.ColorPickerItem; -import com.smartgwt.client.widgets.form.fields.ComboBoxItem; -import com.smartgwt.client.widgets.form.fields.TextItem; - -import com.smartgwt.client.widgets.events.ClickEvent; -import com.smartgwt.client.widgets.events.ClickHandler; -import com.smartgwt.client.widgets.form.events.ItemChangedEvent; -import com.smartgwt.client.widgets.form.events.ItemChangedHandler; -import com.smartgwt.client.types.Alignment; - -import de.intevation.flys.client.shared.model.Collection; -import de.intevation.flys.client.shared.model.CollectionItemAttribute; -import de.intevation.flys.client.shared.model.Style; -import de.intevation.flys.client.shared.model.StyleSetting; -import de.intevation.flys.client.shared.model.FacetRecord; - -import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync; -import de.intevation.flys.client.client.services.CollectionItemAttributeService; -import de.intevation.flys.client.client.ui.CollectionView; - -import de.intevation.flys.client.client.FLYSConstants; -import de.intevation.flys.client.client.Config; - -/** - * @author Raimund Renkert - */ -public class StyleEditorWindow -extends Window -implements ClickHandler -{ - /** The interface that provides i18n messages. */ - protected FLYSConstants MSG = GWT.create(FLYSConstants.class); - - /** The collection */ - protected Collection collection; - - /** The parent collection view */ - protected CollectionView view; - - /** The attributes */ - protected CollectionItemAttribute attributes; - - /** The selected facet. */ - protected FacetRecord facet; - - /** Main layout */ - protected VLayout layout; - - /** The service used to set collection item attributes. */ - protected CollectionItemAttributeServiceAsync itemAttributeService = - GWT.create(CollectionItemAttributeService.class); - - public StyleEditorWindow ( - Collection collection, - CollectionItemAttribute attributes, - FacetRecord facet) - { - this.collection = collection; - this.attributes = attributes; - this.facet = facet; - this.layout = new VLayout(); - - init(); - initPanels(); - } - - - protected void init() { - setTitle(MSG.properties()); - setWidth(270); - setHeight(200); - setCanDragReposition(true); - setCanDragResize(true); - setKeepInParentRect(true); - - layout.setWidth100(); - layout.setHeight100(); - - } - - - protected void initPanels() { - HLayout buttons = new HLayout(); - Button accept = new Button(MSG.label_ok()); - Button cancel = new Button(MSG.label_cancel()); - cancel.addClickHandler(this); - accept.addClickHandler(new ClickHandler() { - public void onClick(ClickEvent e) { - saveStyle(); - } - }); - - buttons.addMember(accept); - buttons.addMember(cancel); - buttons.setAlign(Alignment.RIGHT); - - layout.addMember(createPropertyGrid());; - layout.addMember(buttons); - addItem(layout); - } - - - public void setCollectionView (CollectionView view) { - this.view = view; - setParentElement(this.view.getParentElement()); - } - - - public void onClick(ClickEvent event) { - this.hide(); - } - - - protected VLayout createPropertyGrid() { - VLayout properties = new VLayout(); - - Style s = attributes.getStyle(facet.getTheme().getFacet()); - - TextItem name = new TextItem("name", "Name"); - name.setValue(facet.getName()); - name.setTitleStyle("color:#000; width:120px"); - name.setTitleAlign(Alignment.LEFT); - name.setDisabled(true); - name.setShowDisabled(false); - DynamicForm f = new DynamicForm(); - f.setFields(name); - properties.addMember(f); - - for (int i = 0; i < s.getNumSettings(); i ++) { - final StyleSetting set = s.getSetting(i); - DynamicForm property = createPropertyUI( - set.getDisplayName(), - set.getName(), - set.getType(), - set.getDefaultValue()); - properties.addMember(property); - } - return properties; - } - - protected DynamicForm createPropertyUI( - String dname, - String name, - String type, - String value) - { - DynamicForm df = new DynamicForm(); - - FormItem f; - if(type.equals("int")) { - SpinnerItem s = new SpinnerItem(name, dname); - s.setMin(1); - s.setMax(10); - s.setValue(value); - f = s; - } - else if (type.equals("boolean")) { - CheckboxItem c = new CheckboxItem(name, dname); - if(value.equals("true")) { - c.setValue(true); - } - else { - c.setValue(false); - } - c.setLabelAsTitle(true); - f = c; - } - else if (type.equals("Color")) { - ColorPickerItem c = new ColorPickerItem(name, dname); - c.setValue(rgbToHtml(value)); - f = c; - } - else if (type.equals("Dash")) { - f = new ComboBoxItem(name, dname); - f.setValue(value); - } - else { - f = new FormItem(); - } - f.setTitleStyle("color:#000; width:120px"); - f.setTitleAlign(Alignment.LEFT); - df.setFields(f); - df.addItemChangedHandler(new ItemChangedHandler() { - public void onItemChanged(ItemChangedEvent e) { - String name = e.getItem().getName(); - String newValue = e.getNewValue().toString(); - GWT.log("changed: " + name); - setNewValue(name, newValue); - } - }); - - return df; - } - - protected String rgbToHtml(String rgb) { - String[] parts = rgb.split(","); - int values[] = new int[parts.length]; - for (int i = 0; i < parts.length; i++) { - parts[i] = parts[i].trim(); - try { - values[i] = Integer.parseInt(parts[i]); - } - catch(NumberFormatException nfe) { - return "#000000"; - } - } - String hex = "#"; - for (int i = 0; i < values.length; i++) { - if (values[i] < 16) { - hex += "0"; - } - hex += Integer.toHexString(values[i]); - } - return hex; - } - - protected String htmlToRgb(String html) { - if (!html.startsWith("#")) { - return "0, 0, 0"; - } - - int r = Integer.valueOf(html.substring(1, 3), 16); - int g = Integer.valueOf(html.substring(3, 5), 16); - int b = Integer.valueOf(html.substring(5, 7), 16); - - return r + ", " + g + ", " + b; - } - - protected void saveStyle () { - GWT.log("StyleEditorWindow.saveStyle()"); - Config config = Config.getInstance(); - String url = config.getServerUrl(); - String locale = config.getLocale(); - - itemAttributeService.setCollectionItemAttribute( - this.collection, - attributes.getArtifact(), - url, - locale, - attributes, - new AsyncCallback() { - public void onFailure (Throwable caught) { - GWT.log("Could not set Collection item attributes."); - } - public void onSuccess(Void v) { - GWT.log("Successfully saved collection item attributes."); - } - }); - - - this.hide(); - } - - protected final void setNewValue(String name, String value) { - Style s = attributes.getStyle(facet.getTheme().getFacet()); - StyleSetting set = s.getSetting(name); - if(name.equals("linecolor")) { - value = htmlToRgb(value); - } - set.setDefaultValue(value); - } -} -// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :