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