001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.ui.components.gsidebar; 005 006 007 import graphlab.platform.core.BlackBoard; 008 import graphlab.ui.components.gbody.GBody; 009 010 import javax.swing.*; 011 import javax.swing.border.LineBorder; 012 import java.awt.*; 013 import java.awt.event.ActionEvent; 014 import java.awt.event.ActionListener; 015 import java.net.URL; 016 017 /** 018 * this class is the sidebar part of GFrame 019 * Author: Azin Azadi 020 * Email: azin_azadi at users.sourceforge.net 021 */ 022 public class GSidebar extends JPanel { 023 /** 024 * 025 */ 026 private static final long serialVersionUID = -599129598320886343L; 027 private final int sidebarWidth = 20; 028 private GBody targetBody; 029 030 /** 031 * constructor 032 */ 033 public GSidebar(GBody targetBody, BlackBoard blackboard) { 034 this.targetBody = targetBody; 035 initComponents(); 036 } 037 038 private void initComponents() { 039 // setLayout(new FlowLayout(FlowLayout.CENTER)); 040 // setPreferredSize(new Dimension(sidebarWidth, 100)); 041 // FlowLayout f = new FlowLayout(FlowLayout.CENTER, 0, 2); 042 // setLayout(f); 043 // GridLayout mgr = new GridLayout(5, 1, 1, 2); 044 // mgr. 045 046 BoxLayout mgr = new BoxLayout(this, BoxLayout.Y_AXIS); 047 setLayout(mgr); 048 049 // setLayout(mgr); 050 //Lay out the buttons in one row and as many columns 051 //as necessary, with 6 pixels of padding all around. 052 // SwingUtilities.makeCompactGrid(contentPane, 1, 053 // contentPane.getComponentCount(), 054 // 6, 6, 6, 6); 055 056 // setBorder(new EmptyBorder(2, 2, 0, 1)); 057 // setMaximumSize(getPreferredSize()); 058 // setSize(getPreferredSize()); 059 // hidePanel(); 060 } 061 062 /** 063 * adds a button to side bar, and match it to component 064 */ 065 public void addButton(String iconFileName, Component component, String label) { 066 addButton(new ImageIcon(iconFileName), component, label); 067 } 068 069 public void addButton(URL iconURl, Component component, String label) { 070 addButton(new ImageIcon(iconURl), component, label); 071 } 072 073 public void addButton(Icon icon, Component c, String label) { 074 System.out.println("Adding ShellSideBar with Icon : " + icon); 075 GSidebarButton b = new GSidebarButton(icon, c, this, label); 076 add(b); 077 validate(); 078 // setPanel(c); 079 // b.setMargin(new Insets(1, 1, 1, 1)); 080 } 081 082 /** 083 * sets the currently viewable panel 084 */ 085 public void setPanel(Component sidePanel, String label) { 086 targetBody.showSideBarPane(sidePanel, label); 087 } 088 089 /** 090 * hides the panel from the screen 091 */ 092 public void hidePanel() { 093 targetBody.hideSideBar(); 094 } 095 } 096 097 /** 098 * java currently (now we have jdk 1.5) does not supporots "Vertical Text" 099 * yes, yes, i know that it's a shame for java. 100 * i searched the internet for some hours to find that how can i write a String vertically in any way on the screen 101 * you know that the side bar buttons are vertically, and the texts in JButtons is aligned vertically 102 * I suggested some ways for doing this. 103 * 1- the texts in the swing can be HTML, i mean that you can write for ex. b.setText("<html><font color="red">asda</font></html>"); 104 * we used this method in hour vertices to color out texts. 105 * so if we have the ability of writing text vertically in HTML, then we can use this to have the vertical buttons, 106 * i had sawn vertical texts, in some pages using Internet Explorer. but when i search for such a future , it seems 107 * that this feautre is not a standard HTML feature, and just supported by IE. 108 * 2- we can build a system from base, like the current system. current sytem write horizentally, and we can build our 109 * system writing vertically, independent of the old system. 110 * this way is hard to implement. it needs a great amount of work to support all the features of such a system. 111 * when i was searching the internet for finding a solution, i find a project for writing the texts vertically , and 112 * it used the way i said above, and also it was free! 113 * but i didn't like to use the system in GraphLab project. because i think that it adds too complexitiy to GraphLab 114 * and also i think that the one that should do the jub is SUN. so i think that it is better that wait until the sun 115 * implement this feature, and i preffered a third solution. 116 * 3- our slide bar button will not more than 10. so simply we can just take a picture from input and display it 117 * , so the user can put every thing on the picture including vertical texts. 118 * 4- and there is another way , that i think it is a hack! 119 * we can write the texts horizontally , then take a picture of it, and then simply rotate the text. :D 120 * but in graphlab i preffer to use the 3rd way because of its simplicity. 121 * -------------- 122 * 5- roozbeh suggests a solution for the problem. he shows the Sun Java 2D demos, that are in the JDK Demos. there 123 * was a demo of fonts in java2d. it seems that it can be very simple to rotate the texts in java2d. 124 */ 125 class GVerticalButton extends JToggleButton { 126 127 /** 128 * 129 */ 130 private static final long serialVersionUID = 4372060475633555488L; 131 //todo: inja hast ta badan por beshe :D. (to be filled later) 132 133 134 } 135 136 //fek mikonam age az jense togle button bashan behtar bashe 137 138 class GSidebarButton extends GVerticalButton implements ActionListener { 139 /** 140 * 141 */ 142 private static final long serialVersionUID = -3299575618889083096L; 143 private Component sidePanel; 144 private GSidebar sidebar; 145 private String label; 146 147 public GSidebarButton(Icon icon, Component sidepanel, GSidebar sidebar, String label) { 148 //todo: in sidePanel shaiad lazem she jaie JPanel ie chizi too maiehaie sidebarpanel bashe. felan ke lozoomi nemibinam 149 this.sidePanel = sidepanel; 150 this.sidebar = sidebar; 151 this.label = label; 152 setIcon(icon); 153 154 if (icon.getIconHeight() == -1) //if the icon was not loaded succesfully 155 setText("|"); 156 setBorder(new LineBorder(Color.gray, 1, true)); 157 //setPreferredSize(new Dimension(icon.getIconWidth() + 2, 2 + icon.getIconHeight())); 158 addActionListener(this); 159 } 160 161 /** 162 * Invoked when an action occurs. 163 */ 164 public void actionPerformed(ActionEvent e) { 165 // if (super.isSelected()) 166 sidebar.setPanel(sidePanel, label); 167 // else 168 // sidebar.hidePanel(); 169 } 170 171 }