PageRenderTime 47ms CodeModel.GetById 15ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/edu/uncc/parsets/gui/SideBar.java

https://code.google.com/p/parsets/
Java | 334 lines | 240 code | 61 blank | 33 comment | 25 complexity | 054fa1eb071432719fadc027e46034cb MD5 | raw file
  1package edu.uncc.parsets.gui;
  2
  3import it.cnr.imaa.essi.lablib.gui.checkboxtree.CheckboxTree;
  4import it.cnr.imaa.essi.lablib.gui.checkboxtree.DefaultCheckboxTreeCellRenderer;
  5import it.cnr.imaa.essi.lablib.gui.checkboxtree.TreeCheckingEvent;
  6import it.cnr.imaa.essi.lablib.gui.checkboxtree.TreeCheckingListener;
  7import it.cnr.imaa.essi.lablib.gui.checkboxtree.TreeCheckingModel.CheckingMode;
  8
  9import java.awt.BorderLayout;
 10import java.awt.Dimension;
 11import java.awt.FontMetrics;
 12import java.awt.Graphics;
 13import java.awt.Graphics2D;
 14import java.awt.Insets;
 15import java.awt.Rectangle;
 16import java.awt.event.ActionEvent;
 17import java.awt.event.ActionListener;
 18import java.awt.geom.AffineTransform;
 19import java.awt.event.ItemListener;
 20import java.awt.event.ItemEvent;
 21import java.awt.Checkbox;
 22
 23import javax.swing.BorderFactory;
 24import javax.swing.Box;
 25import javax.swing.BoxLayout;
 26import javax.swing.Icon;
 27import javax.swing.JButton;
 28import javax.swing.JComponent;
 29import javax.swing.JLabel;
 30import javax.swing.JPanel;
 31import javax.swing.JScrollPane;
 32import javax.swing.JTabbedPane;
 33import javax.swing.event.ChangeEvent;
 34import javax.swing.event.ChangeListener;
 35import javax.swing.plaf.basic.BasicLabelUI;
 36import javax.swing.tree.DefaultMutableTreeNode;
 37import javax.swing.tree.DefaultTreeModel;
 38
 39
 40import net.miginfocom.swing.MigLayout;
 41import edu.uncc.parsets.data.CategoryHandle;
 42import edu.uncc.parsets.data.DataSet;
 43import edu.uncc.parsets.data.DimensionHandle;
 44import edu.uncc.parsets.util.osabstraction.AbstractOS;
 45
 46/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 47 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
 48 *                     and others (see Authors.txt for full list)
 49 * All rights reserved.
 50 * 
 51 * Redistribution and use in source and binary forms, with or without
 52 * modification, are permitted provided that the following conditions are met:
 53 * 
 54 *    * Redistributions of source code must retain the above copyright
 55 *      notice, this list of conditions and the following disclaimer.
 56 *    * Redistributions in binary form must reproduce the above copyright
 57 *      notice, this list of conditions and the following disclaimer in the
 58 *      documentation and/or other materials provided with the distribution.
 59 *    * Neither the name of UNC Charlotte nor the names of its contributors
 60 *      may be used to endorse or promote products derived from this software
 61 *      without specific prior written permission.
 62 *      
 63 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
 64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 66 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 73\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 74@SuppressWarnings("serial")
 75public class SideBar extends JPanel implements DataSetListener {
 76
 77    private static final String DATASETTABTITLE = "Data Set";
 78    private static int dataSetTabNumber = 2;
 79    private CheckboxTree dimTree;
 80    private Controller controller;
 81    private JTabbedPane tabs;
 82    private JPanel dataSetTab;
 83    private DBTab dbTab;
 84    private OnlineDataTab onlineDataTab;
 85    private JLabel dsLabel;
 86
 87    // from http://www.codeguru.com/java/articles/199.shtml
 88    private static class VerticalLabelUI extends BasicLabelUI {
 89
 90        private static final int PADDING = 5;
 91
 92        VerticalLabelUI() {
 93            super();
 94        }
 95
 96        public Dimension getPreferredSize(JComponent c) {
 97            Dimension dim = super.getPreferredSize(c);
 98            return new Dimension(dim.height, dim.width + 2 * PADDING + 2);
 99        }
100        private Rectangle paintIconR = new Rectangle();
101        private Rectangle paintTextR = new Rectangle();
102        private Rectangle paintViewR = new Rectangle();
103        private Insets paintViewInsets = new Insets(0, 0, 0, 0);
104
105        public void paint(Graphics g, JComponent c) {
106
107            JLabel label = (JLabel) c;
108            String text = label.getText();
109            Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
110
111            if ((icon == null) && (text == null)) {
112                return;
113            }
114
115            FontMetrics fm = g.getFontMetrics();
116            paintViewInsets = c.getInsets(paintViewInsets);
117
118            paintViewR.x = paintViewInsets.left;
119            paintViewR.y = paintViewInsets.top;
120
121            // Use inverted height & width
122            paintViewR.height = c.getWidth()
123                    - (paintViewInsets.left + paintViewInsets.right);
124            paintViewR.width = c.getHeight()
125                    - (paintViewInsets.top + paintViewInsets.bottom);
126
127            paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
128            paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
129
130            String clippedText = layoutCL(label, fm, text, icon, paintViewR,
131                    paintIconR, paintTextR);
132
133            Graphics2D g2 = (Graphics2D) g;
134            AffineTransform tr = g2.getTransform();
135            g2.rotate(-Math.PI / 2);
136            g2.translate(-c.getHeight(), 0);
137
138            if (icon != null) {
139                icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
140            }
141
142            int textX = paintTextR.x + PADDING;
143            int textY = paintTextR.y + fm.getAscent();
144
145            if (label.isEnabled()) {
146                paintEnabledText(label, g, clippedText, textX, textY);
147            } else {
148                paintDisabledText(label, g, clippedText, textX, textY);
149            }
150
151            g2.setTransform(tr);
152        }
153    }
154
155    public SideBar(final MainWindow mainWin, Controller mainController) {
156        super(new BorderLayout());
157
158        controller = mainController;
159        controller.addDataSetListener(this);
160
161        tabs = new JTabbedPane();
162        tabs.setTabPlacement(JTabbedPane.LEFT);
163        add(tabs, BorderLayout.CENTER);
164
165        onlineDataTab = new OnlineDataTab(mainWin);
166        tabs.addTab(OnlineDataTab.TABTITLE, onlineDataTab);
167        makeRotatedLabel(tabs, 0, OnlineDataTab.TABTITLE);
168
169        dbTab = new DBTab(mainWin, controller);
170        tabs.addTab(DBTab.TABTITLE, dbTab);
171        makeRotatedLabel(tabs, 1, DBTab.TABTITLE);
172
173        dataSetTab = makeDataSetTab();
174
175        tabs.addTab(DATASETTABTITLE, dataSetTab);
176        dsLabel = makeRotatedLabel(tabs, 2, DATASETTABTITLE);
177        if (dsLabel != null) {
178            dsLabel.setEnabled(false);
179        }
180        tabs.setEnabledAt(dataSetTabNumber, false);
181        tabs.setSelectedIndex(1);
182
183        tabs.addChangeListener(new ChangeListener() {
184
185            @Override
186            public void stateChanged(ChangeEvent e) {
187                mainWin.setDSMenuItemsEnabled(false);
188            }
189        });
190    }
191
192    public SideBar(DataSet dataset, final AbstractMainView mainPanel, Controller mainController) {
193        super(new BorderLayout());
194
195        controller = mainController;
196        controller.addDataSetListener(this);
197
198        tabs = new JTabbedPane();
199        tabs.setTabPlacement(JTabbedPane.LEFT);
200        add(tabs, BorderLayout.CENTER);
201        dataSetTabNumber = 0;
202
203        dataSetTab = makeDataSetTab();
204
205        tabs.addTab(DATASETTABTITLE, dataSetTab);
206        dsLabel = makeRotatedLabel(tabs, 0, DATASETTABTITLE);
207        if (dsLabel != null) {
208            dsLabel.setEnabled(true);
209        }
210        //tabs.setEnabledAt(0, false);
211        tabs.setSelectedIndex(dataSetTabNumber);
212
213
214        tabs.addChangeListener(new ChangeListener() {
215
216            @Override
217            public void stateChanged(ChangeEvent e) {
218                mainPanel.setDSMenuItemsEnabled(false);
219            }
220        });
221
222        setDataSet(dataset);
223    }
224
225    private JPanel makeDataSetTab() {
226        JPanel p = new JPanel(new MigLayout("wrap 1,fillx,insets 0", "[]", "[grow,fill]r[]r"));
227        p.setOpaque(false);
228
229        Box b = new Box(BoxLayout.Y_AXIS);
230        b.setBorder(BorderFactory.createTitledBorder("Dimensions"));
231        b.setOpaque(false);
232
233        b.add(createDimensionsBox());
234        p.add(b, "growx");
235
236        JButton clearButton = new JButton("Clear Canvas");
237        clearButton.addActionListener(new ActionListener() {
238
239            public void actionPerformed(ActionEvent e) {
240                controller.parSetsView.clearScreen();
241                dimTree.clearChecking();
242            }
243        });
244        p.add(clearButton, "center");
245        
246        Checkbox mode = new Checkbox("Change Mode");
247        mode.addItemListener(new ItemListener(){
248        	public void itemStateChanged(ItemEvent L) {
249        		controller.parSetsView.changeState();
250        	}
251        });
252        p.add(mode, "center");
253        
254        Checkbox mode2 = new Checkbox("Basic/Curved Ribbons");
255        mode2.addItemListener(new ItemListener(){
256        	public void itemStateChanged(ItemEvent L){
257        		controller.parSetsView.changeBarState();
258        	}
259        });
260        p.add(mode2, "center");
261        
262        return p;
263    }
264
265    private JLabel makeRotatedLabel(JTabbedPane tabs, int index, String tabtitle) {
266        if (!AbstractOS.getCurrentOS().isMacOSX()) {
267            JLabel label = new JLabel(tabtitle);
268            label.setUI(new VerticalLabelUI());
269            tabs.setTabComponentAt(index, label);
270            return label;
271        } else {
272            return null;
273        }
274    }
275
276    public void setDataSet(DataSet data) {
277        dimTree.setModel(new DefaultTreeModel(data.getCategoricalDimensionsAsTree()));
278        tabs.setEnabledAt(dataSetTabNumber, true);
279        String name = data.getName();
280        if (name.length() > 10) {
281            tabs.setToolTipTextAt(dataSetTabNumber, name);
282            name = name.substring(0, 10) + "\u2026";
283        } else {
284            tabs.setToolTipTextAt(dataSetTabNumber, null);
285        }
286        tabs.setTitleAt(dataSetTabNumber, name);
287        if (dsLabel != null) {
288            dsLabel.setText(name);
289            dsLabel.setEnabled(true);
290        }
291        tabs.setSelectedComponent(dataSetTab);
292    }
293
294    private JComponent createDimensionsBox() {
295//		dimBox.setBorder(BorderFactory.createTitledBorder("Categorical"));
296        dimTree = new CheckboxTree(new DefaultMutableTreeNode());
297        dimTree.setRootVisible(false);
298
299        // Remove the folder and file icons.
300        ((DefaultCheckboxTreeCellRenderer) dimTree.getCellRenderer()).setLeafIcon(null);
301        ((DefaultCheckboxTreeCellRenderer) dimTree.getCellRenderer()).setClosedIcon(null);
302        ((DefaultCheckboxTreeCellRenderer) dimTree.getCellRenderer()).setOpenIcon(null);
303
304        dimTree.getCheckingModel().setCheckingMode(CheckingMode.PROPAGATE_PRESERVING_UNCHECK);
305
306        dimTree.addTreeCheckingListener(new TreeCheckingListener() {
307
308            public void valueChanged(TreeCheckingEvent event) {
309
310                DefaultMutableTreeNode node = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();
311
312                if (node.isLeaf()) {
313                    if (event.isCheckedPath()) {
314                        controller.parSetsView.addCategory((DimensionHandle) ((DefaultMutableTreeNode) node.getParent()).getUserObject(),
315                                (CategoryHandle) node.getUserObject());
316                    } else {
317                        controller.parSetsView.removeCategory((DimensionHandle) ((DefaultMutableTreeNode) node.getParent()).getUserObject(),
318                                (CategoryHandle) node.getUserObject());
319                    }
320                } else if (!node.isRoot()) {
321                    if (event.isCheckedPath()) {
322                        controller.parSetsView.addAxis((DimensionHandle) node.getUserObject());
323                    } else {
324                        controller.parSetsView.removeAxis((DimensionHandle) node.getUserObject());
325                    }
326                }
327            }
328        });
329
330        JScrollPane dimPane = new JScrollPane(dimTree);
331
332        return dimPane;
333    }
334}