View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * Vectomatic is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.client.rep.view;
19  
20  import org.vectomatic.client.rep.RepApplication;
21  import org.vectomatic.client.rep.controller.ColorEditor;
22  import org.vectomatic.common.model.style.Color;
23  import org.vectomatic.common.model.style.Palette;
24  import org.vectomatic.common.model.style.PaletteList;
25  
26  import com.google.gwt.user.client.ui.Button;
27  import com.google.gwt.user.client.ui.ChangeListener;
28  import com.google.gwt.user.client.ui.ClickListener;
29  import com.google.gwt.user.client.ui.DialogBox;
30  import com.google.gwt.user.client.ui.FlexTable;
31  import com.google.gwt.user.client.ui.Label;
32  import com.google.gwt.user.client.ui.ListBox;
33  import com.google.gwt.user.client.ui.ScrollPanel;
34  import com.google.gwt.user.client.ui.TextBox;
35  import com.google.gwt.user.client.ui.Widget;
36  
37  /**
38   * Widget class to edit palettes
39   */
40  public class PaletteEditor extends DialogBox implements ChangeListener {
41  	private Button _newPaletteButton;
42  	private Button _clonePaletteButton;
43  	private Button _deletePaletteButton;
44  	private Button _editColorButton;
45  	private Button _newColorButton;
46  	private Button _deleteColorButton;
47  	private Button _closeButton;
48  	private ListBox _paletteList;
49  	private TextBox _paletteName;
50  	private PaletteList _palettes;
51  	private PaletteWidget _paletteWidget;
52  	private ColorEditor _colorEditor;
53  	private PlainColorMenu _menu;
54  	private static int _count;
55  	private RepApplication _app;
56  	
57  	public PaletteEditor(RepApplication app, ColorEditor colorEditor, PlainColorMenu menu) {
58  		super();
59  		_app = app;
60  		setText(_app.getConstants().paletteEditorPanel());
61  		_palettes = _app.getPalettes();
62  		_colorEditor = colorEditor;
63  		_menu = menu;
64  
65  		_newPaletteButton = new Button(_app.getConstants().newPaletteButton(), new ClickListener() {
66  			public void onClick(Widget sender) {
67  				newPalette();
68  			}
69  		});
70  		_newPaletteButton.setWidth("100%");
71  
72  		_clonePaletteButton = new Button(_app.getConstants().clonePaletteButton(), new ClickListener() {
73  			public void onClick(Widget sender) {
74  				clonePalette();
75  			}
76  		});
77  		_clonePaletteButton.setWidth("100%");
78  		
79  		_deletePaletteButton = new Button(_app.getConstants().deletePaletteButton(), new ClickListener() {
80  			public void onClick(Widget sender) {
81  				deletePalette();
82  			}
83  		});
84  		_deletePaletteButton.setWidth("100%");
85  
86  		_editColorButton = new Button(_app.getConstants().editColorButton(), new ClickListener() {
87  			public void onClick(Widget sender) {
88  				editColor();
89  			}
90  		});
91  		_editColorButton.setWidth("100%");
92  		
93  		_newColorButton = new Button(_app.getConstants().newColorButton(), new ClickListener() {
94  			public void onClick(Widget sender) {
95  				newColor();
96  			}
97  		});
98  		_newColorButton.setWidth("100%");
99  		
100 		_deleteColorButton = new Button(_app.getConstants().deleteColorButton(), new ClickListener() {
101 			public void onClick(Widget sender) {
102 				deleteColor();
103 			}
104 		});
105 		_deleteColorButton.setWidth("100%");
106 		
107 		_closeButton = new Button(_app.getConstants().closeButton(), new ClickListener() {
108 			public void onClick(Widget sender) {
109 				close();
110 			}
111 		});
112 		_closeButton.setWidth("100%");
113 		
114 		_paletteList = new ListBox();
115 		_paletteList.setWidth("100%");
116 		_paletteList.setHeight("100%");
117 		_paletteList.setVisibleItemCount(5);
118 		for (int i = 0, size = _palettes.size(); i < size; i++) {
119 			_paletteList.addItem(_palettes.getPalette(i).getName());
120 		}
121 		
122 		_paletteName = new TextBox();
123 		_paletteName.setWidth("100%");
124 		_paletteName.setMaxLength(32);
125 		_paletteName.addChangeListener(new ChangeListener() {
126 			public void onChange(Widget sender) {
127 				if (_paletteName.getText().length() > 0) {
128 					_paletteWidget.getPalette().setName(_paletteName.getText());
129 					_paletteList.setItemText(_paletteList.getSelectedIndex(), _paletteName.getText());
130 				} else {
131 					_paletteName.setText(_paletteWidget.getPalette().getName());
132 				}
133 			}
134 		});
135 		_paletteList.addChangeListener(this);
136 		_paletteWidget = new PaletteWidget(null, _colorEditor);
137 		if (_palettes.size() > 0) {
138 			_paletteList.setSelectedIndex(0);
139 			onChange(_paletteList);
140 		}
141 		FlexTable table = new FlexTable();
142 		table.setWidget(0, 0, _paletteList);
143 		table.setWidget(0, 1, new Label(_app.getConstants().paletteNameLabel()));
144 		table.setWidget(1, 0, _paletteName);
145 		ScrollPanel scrollPanel = new ScrollPanel(_paletteWidget);
146 		scrollPanel.setWidth("195px");
147 		scrollPanel.setHeight("125px");
148 		table.setWidget(2, 0, scrollPanel);
149 		table.setWidget(3, 0, _newPaletteButton);
150 		table.setWidget(3, 1, _newColorButton);
151 		table.setWidget(4, 0, _clonePaletteButton);
152 		table.setWidget(4, 1, _editColorButton);
153 		table.setWidget(5, 0, _deletePaletteButton);
154 		table.setWidget(5, 1, _deleteColorButton);
155 		table.setWidget(6, 1, _closeButton);
156 		table.getFlexCellFormatter().setRowSpan(0, 0, 3);
157 		table.getColumnFormatter().setWidth(0, "195px");
158 		table.getColumnFormatter().setWidth(1, "195px");
159 		setWidget(table);
160 	}
161 	
162 	
163 	private void newPalette() {
164 		Palette palette = _palettes.newPalette();
165 		palette.setName(_app.getConstants().paletteBaseName() + _count++);
166 		_paletteList.addItem(palette.getName());
167 		_paletteList.setSelectedIndex(_palettes.size() - 1);
168 		onChange(_paletteList);
169 	}
170 	
171 	private void deletePalette() {
172 		int index = _paletteList.getSelectedIndex();
173 		_palettes.removePalette(index);
174 		_paletteList.removeItem(index);
175 		index = Math.min(_paletteList.getItemCount() - 1, index);
176 		if (index != -1) {
177 			_paletteList.setSelectedIndex(index);
178 		}
179 		onChange(_paletteList);
180 	}
181 	
182 	private void clonePalette() {
183 		int index = _paletteList.getSelectedIndex();
184 		Palette clone = _palettes.clonePalette(index);
185 		clone.setName(_app.getConstants().copyOf() + _palettes.getPalette(index).getName());
186 		_paletteList.addItem(clone.getName());
187 		_paletteList.setSelectedIndex(_palettes.size() - 1);
188 		onChange(_paletteList);
189 	}
190 	
191 	private void newColor() {
192 		_paletteWidget.getPalette().addColor(Color.fromRGB(255, 255, 255));
193 		refresh();
194 	}
195 	
196 	private void deleteColor() {
197 		int index = _paletteWidget.getSelectedColorIndex();
198 		Palette palette = _paletteWidget.getPalette();
199 		palette.removeColor(index);
200 		index = Math.min(palette.getSize() - 1, index);
201 		_paletteWidget.selectColor(index);
202 		refresh();
203 	}
204 	
205 	private void editColor() {
206 		_colorEditor.setColor(_paletteWidget.getPalette().getColor(_paletteWidget.getSelectedColorIndex()), new ChangeListener() {
207 			public void onChange(Widget sender) {
208 				Palette palette = _paletteWidget.getPalette();
209 				int index = _paletteWidget.getSelectedColorIndex();
210 				_colorEditor.getColor(palette.getColor(index));
211 				palette.setColor(index, palette.getColor(index));
212 			}		
213 		});
214 		_colorEditor.show();
215 	}
216 	
217 	private void close() {
218 		hide();
219 		_menu.setPalette(getSelectedPalette());
220 	}
221 
222 	public void onChange(Widget sender) {
223 		Palette palette = getSelectedPalette();
224 		_paletteWidget.setPalette(palette);
225 		_paletteName.setText(palette != null ? palette.getName() : "");
226 		refresh();
227 	}
228 	
229 	private void refresh() {
230 		int index = _paletteList.getSelectedIndex();
231 		_clonePaletteButton.setEnabled(index != -1);
232 		_deletePaletteButton.setEnabled((index != -1) && (_palettes.size() > 1));
233 		if (index != -1 && (_palettes.getPalette(index) != _paletteWidget.getPalette())) {
234 			_paletteWidget.setPalette(_palettes.getPalette(index));
235 		}
236 		_paletteName.setEnabled(index != -1);
237 		Palette palette = (index != -1) ? _palettes.getPalette(index) : null;
238 		_deleteColorButton.setEnabled((palette != null) && (_paletteWidget.getSelectedColorIndex() != -1));
239 		_newColorButton.setEnabled(palette != null);
240 		_editColorButton.setEnabled((palette != null) && (_paletteWidget.getSelectedColorIndex() != -1));
241 	}
242 	
243 	public Palette getSelectedPalette() {
244 		int index = _paletteList.getSelectedIndex();
245 		return (index != -1) ? _palettes.getPalette(index) : null;
246 	}
247 }