1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.IconUIResource;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import org.jdom2.Element;
import com.hraccess.V4DS.loader.Groupe;
public class DataDisplayer extends JFrame {
private JPanel jPanelTree= new javax.swing.JPanel();
private JPanel jPanelLog= new javax.swing.JPanel();
private JPanel jPanelData= new javax.swing.JPanel();
private JScrollPane jScrollPane = new javax.swing.JScrollPane();
JScrollPane jScrollPaneLeft = new javax.swing.JScrollPane();
private JTree jTree = null ;
private Groupe Racine;
private Element racine;
public DataDisplayer() {
super();
}
public DataDisplayer(Groupe Racine, Element racine) {
super();
this.Racine= Racine;
this.racine= racine;
initialize();
}
/********************************************************************************/
// add MouseListener to tree
MouseAdapter ma = new MouseAdapter() {
private void myPopupEvent(MouseEvent e) {
int x = e.getX();
int y = e.getY();
JTree tree = (JTree)e.getSource();
TreePath path = tree.getPathForLocation(x, y);
if (path == null)
return;
tree.setSelectionPath(path);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
String label = node.toString();
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Add"));
popup.add(new JMenuItem("Remove"));
popup.add(new JMenuItem("Clear"));
popup.show(tree, x, y);
}
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e))
{ System.out.println("Right click Mouse");
myPopupEvent(e);}
}
};
/********************************************************************************/
//Listener left click
TreeSelectionListener TsL = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
jTree.getLastSelectedPathComponent();
String name = e.getNewLeadSelectionPath().getLastPathComponent().toString();
System.out.println(name);
Groupe gp = new Groupe();
gp.FindGpByName(Racine, name);
JLabel jlabel= new JLabel();
String html ="<html><ul>";
for(int i=0;i<Groupe.result.getRubriqueList().size();i++){
html= html+"<li>"+Groupe.result.getRubriqueList().get(i).getName()+" : "
+Groupe.result.getRubriqueList().get(i).getValeur() +"</li><br/>";
jlabel.setFont(new Font("Verdana",4,10));
}
html=html+"</ul></html>";
jlabel.setText(html);
jScrollPaneLeft.getViewport().add(jlabel, null);
TreePath path = e.getPath();
int pathCount = path.getPathCount();
for (int i = 0; i < pathCount; i++) {
System.out.print(path.getPathComponent(i).toString());
if (i + 1 != pathCount) {
System.out.print("|");
}
}
System.out.println("");
}
};
/********************************************************************************/
private void getJTree(Groupe Racine,DefaultMutableTreeNode root) {
for(int i=0;i<Racine.getGroupeList().size();i++)
{
DefaultMutableTreeNode noeud1 = new DefaultMutableTreeNode(Racine.getGroupeList().get(i).getName());
root.add(noeud1);
getJTree(Racine.getGroupeList().get(i), noeud1);
}
}
/********************************************************************************/
private JScrollPane getLeftJScrollPane(JTree jTree) {
jScrollPane.setViewportView(jTree);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return jScrollPane;
}
/********************************************************************************/
public void SetJpanelData(){
DefaultMutableTreeNode root =new DefaultMutableTreeNode(racine.getName());
getJTree(Racine, root);
jTree= new JTree(root);
//Right click
jTree.addMouseListener(ma);
//left Click
jTree.addTreeSelectionListener(TsL);
jPanelTree.setBackground(Color.GREEN);
jPanelTree.setLayout(new java.awt.BorderLayout());
jPanelTree.add(getLeftJScrollPane(jTree), java.awt.BorderLayout.CENTER);
jScrollPaneLeft.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPaneLeft.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPaneLeft.setViewportBorder(new LineBorder(Color.gray));
jScrollPaneLeft.getViewport().setBackground(Color.white);
jPanelData.setLayout(new GridLayout(1,2,5,5));
jPanelData.setPreferredSize(new Dimension(this.getSize().width,3*this.getSize().height/4));
jPanelData.add(jPanelTree);
jPanelData.add(jScrollPaneLeft, null);
}
/********************************************************************************/
public void SetJpanelLog(){
jPanelLog.setBackground(Color.pink);
jPanelLog.setLayout(new java.awt.BorderLayout());
jPanelLog.setPreferredSize(new Dimension(this.getSize().width,this.getSize().height/4));
}
/********************************************************************************/
public void initialize() {
int x= java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
int y =java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
this.setSize(x,y);
this.setLocationRelativeTo(null);
this.getContentPane().setLayout(new java.awt.FlowLayout());
UIManager.put("Tree.collapsedIcon", new IconUIResource(new NodeIcon('+')));
UIManager.put("Tree.expandedIcon", new IconUIResource(new NodeIcon('-')));
SetJpanelData();
SetJpanelLog();
this.setJMenuBar(this.createMenuBar());
this.getContentPane().add(jPanelData);
this.getContentPane().add(jPanelLog);
//this.pack();
this.setTitle("Display Board");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**************************************************************************************************************/
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("Close",
KeyEvent.VK_T);
//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
// ImageIcon icon = createImageIcon("SaveIcon.jpg");
menuItem = new JMenuItem("Close All");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("Save",
KeyEvent.VK_T);
//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem = new JMenuItem("Refresh ");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem("Update ");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("Expand ");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem("Collapse ");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
//a submenu
menu.addSeparator();
submenu = new JMenu("Export As");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem(" Pdf");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem(" Text File");
submenu.add(menuItem);
menu.add(submenu);
menu.addSeparator();
menuItem = new JMenuItem("Exit");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
//Build second menu in the menu bar.
menu = new JMenu("About");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
//Build Third menu in the menu bar.
menu = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
return menuBar;
}
} |
Partager