001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.view;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Component;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049: import java.net.URL;
050: import java.util.ArrayList;
051: import java.util.Arrays;
052: import java.util.Collection;
053: import java.util.Enumeration;
054: import java.util.Iterator;
055: import java.util.List;
056:
057: import javax.swing.DefaultListCellRenderer;
058: import javax.swing.DefaultListModel;
059: import javax.swing.ImageIcon;
060: import javax.swing.JButton;
061: import javax.swing.JList;
062: import javax.swing.JPanel;
063: import javax.swing.JScrollPane;
064: import javax.swing.ListSelectionModel;
065: import net.java.hulp.i18n.Logger;
066: import org.netbeans.modules.etl.logger.Localizer;
067: import org.netbeans.modules.etl.logger.LogUtil;
068:
069: /**
070: * @author Ritesh Adval
071: * @version $Revision$
072: */
073: public class GroupPanel extends JPanel {
074:
075: private static transient final Logger mLogger = LogUtil
076: .getLogger(GroupPanel.class.getName());
077: private static transient final Localizer mLoc = Localizer.get();
078:
079: class ButtonActionListener implements ActionListener {
080:
081: /**
082: * Invoked when an action occurs.
083: */
084: public void actionPerformed(ActionEvent e) {
085: String actionCmd = e.getActionCommand();
086:
087: if (actionCmd == null) {
088: return;
089: }
090:
091: if (actionCmd.equalsIgnoreCase("UP")) {
092: moveUp();
093: } else if (actionCmd.equalsIgnoreCase("DOWN")) {
094: moveDown();
095: }
096: }
097:
098: private void moveDown() {
099: if (listModel.getSize() <= 1) {
100: return;
101: }
102:
103: int[] sel = list.getSelectedIndices();
104:
105: list.clearSelection();
106: Arrays.sort(sel);
107: for (int i = (sel.length - 1); i >= 0; i--) {
108: int index = sel[i];
109: if (index < 0 || index == listModel.size() - 1) {
110: continue;
111: }
112:
113: Object obj = listModel.remove(index);
114: if (obj != null) {
115: listModel.add(index + 1, obj);
116: list.addSelectionInterval(index + 1, index + 1);
117: }
118: }
119: }
120:
121: private void moveUp() {
122: if (listModel.getSize() <= 1) {
123: return;
124: }
125:
126: int[] sel = list.getSelectedIndices();
127:
128: list.clearSelection();
129: Arrays.sort(sel);
130: for (int i = 0; i < sel.length; i++) {
131: int index = sel[i];
132: if (index == 0 || index == listModel.size()) {
133: continue;
134: }
135:
136: Object obj = listModel.remove(index);
137: if (obj != null) {
138: listModel.add(index - 1, obj);
139: list.addSelectionInterval(index - 1, index - 1);
140: }
141: }
142: }
143: }
144:
145: class GroupListCellRenderer extends DefaultListCellRenderer {
146: public Component getListCellRendererComponent(JList aList,
147: Object value, int index, boolean isSelected,
148: boolean cellHasFocus) {
149: return super .getListCellRendererComponent(aList, value
150: .toString(), index, isSelected, cellHasFocus);
151: }
152:
153: }
154:
155: private static final URL DOWN_IMG_URL = GroupPanel.class
156: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/assignDown.png");
157:
158: private static final URL UP_IMG_URL = GroupPanel.class
159: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/assignUp.png");
160: private JList list;
161: private DefaultListModel listModel;
162:
163: /**
164: * New instance
165: *
166: * @param title - title
167: */
168: public GroupPanel(String title) {
169: super ();
170: listModel = new DefaultListModel();
171: initGui(title);
172: }
173:
174: /**
175: * Creates a new instance of GroupByView
176: *
177: * @param title - title
178: * @param columns - columns
179: */
180: public GroupPanel(String title, Collection columns) {
181: this (title);
182: initializeListModel(columns);
183:
184: }
185:
186: /**
187: * Add to list
188: *
189: * @param aList - list
190: */
191: public void addToList(List aList) {
192: Iterator it = aList.iterator();
193: while (it.hasNext()) {
194: Object obj = it.next();
195: if (!contains(obj)) {
196: listModel.addElement(obj);
197: }
198: }
199: }
200:
201: /**
202: * Contains
203: *
204: * @param obj - object
205: * @return true/false
206: */
207: public boolean contains(Object obj) {
208: for (int i = 0; i < listModel.getSize(); i++) {
209: Object listObj = listModel.get(i);
210: if (listObj.equals(obj)) {
211: return true;
212: }
213: }
214:
215: return false;
216: }
217:
218: /**
219: * Get order list
220: *
221: * @return list
222: */
223: public List getOrderedList() {
224: ArrayList orderList = new ArrayList();
225: for (Enumeration e = listModel.elements(); e.hasMoreElements();) {
226: Object element = e.nextElement();
227: orderList.add(element);
228: }
229: return orderList;
230: }
231:
232: /**
233: * Get selected item
234: *
235: * @return list
236: */
237: public List getSelectItems() {
238: ArrayList aList = new ArrayList();
239: Object[] values = list.getSelectedValues();
240: for (int i = 0; i < values.length; i++) {
241: aList.add(values[i]);
242: }
243:
244: return aList;
245: }
246:
247: /**
248: * Remove from list
249: *
250: * @param aList - list
251: */
252: public void removeFromList(List aList) {
253: Iterator it = aList.iterator();
254: while (it.hasNext()) {
255: Object obj = it.next();
256: listModel.removeElement(obj);
257: }
258: }
259:
260: /**
261: * Set data
262: *
263: * @param data - data
264: */
265: public void setData(Collection data) {
266: initializeListModel(data);
267: }
268:
269: private void initGui(String title) {
270: this .setLayout(new BorderLayout());
271:
272: JPanel mainPanel = new JPanel();
273: this .add(mainPanel, BorderLayout.CENTER);
274:
275: mainPanel.setLayout(new BorderLayout());
276:
277: list = new JList(listModel);
278: list.setCellRenderer(new GroupListCellRenderer());
279:
280: list
281: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
282: JScrollPane sPane = new JScrollPane(list);
283: mainPanel.add(sPane, BorderLayout.CENTER);
284:
285: JPanel buttonPanel = new JPanel();
286: GridBagLayout gl = new GridBagLayout();
287: buttonPanel.setLayout(gl);
288: mainPanel.add(buttonPanel, BorderLayout.EAST);
289:
290: GridBagConstraints c = new GridBagConstraints();
291:
292: JButton upButton = new JButton(new ImageIcon(UP_IMG_URL));
293: String nbBundle30 = mLoc.t("PRSR001: UP");
294: upButton.setActionCommand(Localizer.parse(nbBundle30));
295: upButton.getAccessibleContext().setAccessibleName(
296: Localizer.parse(nbBundle30));
297: upButton.setMnemonic(Localizer.parse(nbBundle30).charAt(0));
298:
299: c.gridwidth = GridBagConstraints.REMAINDER;
300: c.weighty = 1.0;
301: c.fill = GridBagConstraints.HORIZONTAL;
302: buttonPanel.add(upButton, c);
303:
304: JButton downButton = new JButton(new ImageIcon(DOWN_IMG_URL));
305: String nbBundle31 = mLoc.t("PRSR001: UP");
306: downButton.setActionCommand(Localizer.parse(nbBundle31));
307: downButton.getAccessibleContext().setAccessibleName(
308: Localizer.parse(nbBundle31));
309: downButton.setMnemonic(Localizer.parse(nbBundle31).charAt(0));
310:
311: c.gridwidth = GridBagConstraints.REMAINDER;
312: c.weighty = 1.0;
313: c.fill = GridBagConstraints.HORIZONTAL;
314: buttonPanel.add(downButton, c);
315:
316: ButtonActionListener aListener = new ButtonActionListener();
317: upButton.addActionListener(aListener);
318: downButton.addActionListener(aListener);
319:
320: }
321:
322: private void initializeListModel(Collection columns) {
323: Iterator it = columns.iterator();
324: while (it.hasNext()) {
325: Object obj = it.next();
326: listModel.addElement(obj);
327: }
328: }
329:
330: }
|