Coverage Report - de.glossmaker.gui.gloss.main.tree.NavigationTreePanel
 
Classes in this File Line Coverage Branch Coverage Complexity
NavigationTreePanel
0%
0/176
0%
0/78
3,238
NavigationTreePanel$1
0%
0/19
0%
0/14
3,238
 
 1  
 package de.glossmaker.gui.gloss.main.tree;
 2  
 
 3  
 import java.awt.BorderLayout;
 4  
 import java.awt.datatransfer.DataFlavor;
 5  
 import java.awt.datatransfer.Transferable;
 6  
 import java.awt.dnd.DnDConstants;
 7  
 import java.awt.dnd.DropTarget;
 8  
 import java.awt.dnd.DropTargetDragEvent;
 9  
 import java.awt.dnd.DropTargetDropEvent;
 10  
 import java.awt.dnd.DropTargetEvent;
 11  
 import java.awt.dnd.DropTargetListener;
 12  
 import java.awt.event.ActionEvent;
 13  
 import java.awt.event.ActionListener;
 14  
 import java.awt.event.InputEvent;
 15  
 import java.awt.event.KeyEvent;
 16  
 import java.awt.event.KeyListener;
 17  
 import java.beans.PropertyChangeEvent;
 18  
 import java.beans.PropertyChangeListener;
 19  
 import java.util.ArrayList;
 20  
 
 21  
 import javax.help.CSH;
 22  
 import javax.swing.JMenuItem;
 23  
 import javax.swing.JPanel;
 24  
 import javax.swing.JPopupMenu;
 25  
 import javax.swing.JScrollPane;
 26  
 import javax.swing.JTree;
 27  
 import javax.swing.KeyStroke;
 28  
 import javax.swing.event.TreeSelectionEvent;
 29  
 import javax.swing.event.TreeSelectionListener;
 30  
 import javax.swing.tree.DefaultTreeCellRenderer;
 31  
 import javax.swing.tree.TreePath;
 32  
 
 33  
 import de.glossmaker.gloss.datastructure.GlossItem;
 34  
 import de.glossmaker.gloss.datastructure.GlossItems;
 35  
 import de.glossmaker.gloss.language.Translation;
 36  
 import de.glossmaker.gloss.listener.EActionCommands;
 37  
 import de.glossmaker.gloss.listener.NavigationTreeMouseListener;
 38  
 import de.glossmaker.gloss.observer.GlossItemsChangePublisher;
 39  
 import de.glossmaker.gloss.observer.SelectionChangePublisher;
 40  
 import de.glossmaker.gui.gloss.bars.HelpItemChangeListener;
 41  
 import de.glossmaker.gui.gloss.main.HelpFocusListener;
 42  
 
 43  
 /**
 44  
  * 
 45  
  * 
 46  
  * @author Markus Flingelli
 47  
  */
 48  0
 public class NavigationTreePanel extends JPanel implements
 49  
                 PropertyChangeListener, ActionListener, KeyListener, DropTargetListener {
 50  
         private static final long serialVersionUID = 3707366253094887171L;
 51  
 
 52  0
         private JScrollPane mTreeScrollPane = null;
 53  0
         private JTree mTree = null;
 54  0
         private JPopupMenu mPopupMenu = null;
 55  0
         private JMenuItem mRemoveItem = null;
 56  0
         private JMenuItem mAddItem = null;
 57  0
         private GlossItems mGlossItems = null;
 58  
 
 59  0
         private String mFileName = null;
 60  0
         private String mSelectedHeading = null;
 61  0
         private GlossItem mSelectedItem = null;
 62  0
         private GlossItemsChangePublisher mGlossItemsChangePublisher = null;
 63  0
         private Translation mTranslation = null;
 64  
         private TreeSelectionListener mTreeSelectionListener;
 65  
 
 66  
         @SuppressWarnings("unused")
 67  
         private DropTarget mDropTarget;
 68  
 
 69  0
         public NavigationTreePanel(GlossItems items) {
 70  0
                 mGlossItems = items;
 71  0
                 mGlossItemsChangePublisher = GlossItemsChangePublisher
 72  
                                 .getInstance(items);
 73  0
                 GlossItemsChangePublisher.getInstance(items).addPropertyChangeListener(
 74  
                                 this);
 75  0
                 mTranslation = Translation.getInstance();
 76  0
                 initialize();
 77  0
         }
 78  
 
 79  
         private void initialize() {
 80  0
                 this.setLayout(new BorderLayout());
 81  0
                 this.add(getTreeScrollPane(), BorderLayout.CENTER);
 82  0
         }
 83  
 
 84  
         private JScrollPane getTreeScrollPane() {
 85  0
                 if (mTreeScrollPane == null) {
 86  0
                         mTreeScrollPane = new JScrollPane();
 87  0
                         mTreeScrollPane.setViewportView(getTree());
 88  
                 }
 89  
 
 90  0
                 return mTreeScrollPane;
 91  
         }
 92  
 
 93  
         private JPopupMenu getPopUpMenu() {
 94  0
                 if (mPopupMenu == null) {
 95  0
                         mPopupMenu = new JPopupMenu();
 96  0
                         mAddItem = new JMenuItem(
 97  
                                         mTranslation.getValue("tree.popup.add_item.text"));
 98  0
                         mAddItem.setToolTipText(mTranslation
 99  
                                         .getValue("tree.popup.add_item.tooltip"));
 100  0
                         mAddItem.setEnabled(false);
 101  0
                         mAddItem.addActionListener(this);
 102  0
                         mAddItem.setActionCommand(EActionCommands.ADD_GLOSS_ITEM.toString());
 103  0
                         mAddItem.addChangeListener(new HelpItemChangeListener(
 104  
                                         "section.navigation_tree"));
 105  0
                         mAddItem.setAccelerator(KeyStroke.getKeyStroke('V',
 106  
                                         InputEvent.CTRL_MASK));
 107  0
                         mPopupMenu.add(mAddItem);
 108  
 
 109  0
                         mRemoveItem = new JMenuItem(
 110  
                                         mTranslation.getValue("tree.popup.remove_item.text"));
 111  0
                         mRemoveItem.setToolTipText(mTranslation
 112  
                                         .getValue("tree.popup.remove_item.tooltip"));
 113  0
                         mRemoveItem.setEnabled(false);
 114  0
                         mRemoveItem.addActionListener(this);
 115  0
                         mRemoveItem.setActionCommand(EActionCommands.REMOVE_GLOSS_ITEM
 116  
                                         .toString());
 117  0
                         mRemoveItem.setAccelerator(KeyStroke.getKeyStroke(
 118  
                                         KeyEvent.VK_DELETE, 0));
 119  0
                         mRemoveItem.addChangeListener(new HelpItemChangeListener(
 120  
                                         "section.navigation_tree"));
 121  0
                         mPopupMenu.add(mRemoveItem);
 122  
 
 123  0
                         mPopupMenu.setOpaque(true);
 124  0
                         mPopupMenu.setLightWeightPopupEnabled(true);
 125  
                 }
 126  
 
 127  0
                 return mPopupMenu;
 128  
         }
 129  
 
 130  
         private JTree getTree() {
 131  0
                 if (mTree == null) {
 132  0
                         mTree = new JTree();
 133  0
                         mTree.setModel(null);
 134  0
                         mTree.setCellEditor(new TreeTextFieldCellEditor(mTree, (DefaultTreeCellRenderer) mTree.getCellRenderer(), mGlossItems));
 135  0
                         mTree.setEditable(true);
 136  0
                         mTree.addMouseListener(new NavigationTreeMouseListener(
 137  
                                         getPopUpMenu()));
 138  0
                         CSH.setHelpIDString(mTree, "section.navigation_tree");
 139  0
                         mTree.addFocusListener(new HelpFocusListener(
 140  
                                         "section.navigation_tree"));
 141  0
                         mTree.addKeyListener(this);
 142  0
                         mTreeSelectionListener = new TreeSelectionListener() {
 143  
 
 144  
                                 @Override
 145  
                                 public void valueChanged(TreeSelectionEvent event) {
 146  0
                                         Object node = mTree.getLastSelectedPathComponent();
 147  0
                                         if (node != null) {
 148  0
                                                 if (node instanceof GlossItem) {
 149  0
                                                         mSelectedItem = (GlossItem) node;
 150  0
                                                         SelectionChangePublisher
 151  
                                                                         .getInstance()
 152  
                                                                         .fireIndexedPropertyChange(
 153  
                                                                                         EActionCommands.TREE_SELECTION_CHANGED
 154  
                                                                                                         .toString(), 0, null,
 155  
                                                                                         mSelectedItem);
 156  0
                                                         if (mRemoveItem != null) {
 157  0
                                                                 mRemoveItem.setEnabled(true);
 158  
                                                         }
 159  
                                                 } else {
 160  0
                                                         mSelectedItem = null;
 161  0
                                                         if (node instanceof String) {
 162  0
                                                                 String heading = (String) node;
 163  0
                                                                 if (heading.equals(mFileName)
 164  
                                                                                 || heading.equals(mTranslation
 165  
                                                                                                 .getValue("tree.root.text"))
 166  
                                                                                 && mRemoveItem != null) {
 167  0
                                                                         mRemoveItem.setEnabled(false);
 168  
                                                                 } else {
 169  0
                                                                         mRemoveItem.setEnabled(true);
 170  0
                                                                         mSelectedHeading = heading;
 171  
                                                                 }
 172  0
                                                         }
 173  
                                                 }
 174  
                                         } else {
 175  0
                                                 mSelectedItem = null;
 176  0
                                                 SelectionChangePublisher
 177  
                                                                 .getInstance()
 178  
                                                                 .fireIndexedPropertyChange(
 179  
                                                                                 EActionCommands.TREE_SELECTION_CHANGED
 180  
                                                                                                 .toString(),
 181  
                                                                                 0, 0, mSelectedItem);
 182  
                                         }
 183  0
                                 }
 184  
                         };
 185  0
                         mTree.addTreeSelectionListener(mTreeSelectionListener);
 186  0
                         mDropTarget = new DropTarget(mTree, this);
 187  
                 }
 188  
 
 189  0
                 return mTree;
 190  
         }
 191  
 
 192  
         private boolean getAction(PropertyChangeEvent event,
 193  
                         EActionCommands actionCommand) {
 194  0
                 return event.getPropertyName().equals(actionCommand.toString());
 195  
         }
 196  
 
 197  
         private void selectTreePath(GlossItem item) {
 198  0
                 if (item != null) {
 199  0
                         GlossItemTreeModel treeModel = (GlossItemTreeModel) mTree
 200  
                                         .getModel();
 201  0
                         TreePath treePath = new TreePath(treeModel.getPathToRoot(item));
 202  0
                         mTree.expandPath(treePath);
 203  0
                         mTree.setSelectionPath(treePath);
 204  0
                         mTree.scrollPathToVisible(treePath);
 205  
                 }
 206  0
         }
 207  
 
 208  
         @SuppressWarnings("unchecked")
 209  
         @Override
 210  
         public void propertyChange(PropertyChangeEvent event) {
 211  0
                 if (getAction(event, EActionCommands.CREATE_NEW_FILE)) {
 212  0
                         mAddItem.setEnabled(true);
 213  0
                         mFileName = mTranslation.getValue("tree.root.text");
 214  0
                         GlossItemTreeModel treeModel = new GlossItemTreeModel(mGlossItems,
 215  
                                         mFileName);
 216  0
                         mTree.setModel(treeModel);
 217  0
                 } else if (getAction(event, EActionCommands.OPEN_FILE)) {
 218  0
                         mFileName = (String) event.getNewValue();
 219  0
                         mAddItem.setEnabled(true);
 220  0
                         GlossItemTreeModel treeModel = new GlossItemTreeModel(mGlossItems,
 221  
                                         mFileName);
 222  0
                         mTree.setModel(treeModel);
 223  0
                 } else if (getAction(event, EActionCommands.SAVE_FILE_AS)) {
 224  0
                         GlossItemTreeModel treeModel = (GlossItemTreeModel) mTree
 225  
                                         .getModel();
 226  0
                         treeModel.setFileName((String) event.getNewValue());
 227  0
                         mTree.updateUI();
 228  0
                         selectTreePath(mSelectedItem);
 229  0
                 } else if (getAction(event, EActionCommands.GLOSS_ITEM_ADDED)) {
 230  0
                         mTree.updateUI();
 231  0
                         selectTreePath((GlossItem) event.getNewValue());
 232  0
                 } else if (getAction(event, EActionCommands.FILE_IMPORTED)) {
 233  0
                         mTree.updateUI();
 234  0
                 } else if (getAction(event, EActionCommands.GLOSS_ITEM_REMOVED)
 235  
                                 || getAction(event, EActionCommands.GLOSS_ITEM_HEADING_REMOVED)) {
 236  0
                         mTree.updateUI();
 237  0
                 } else if (getAction(event, EActionCommands.GLOSS_ITEM_WORD_CHANGED)) {
 238  0
                         mTree.removeTreeSelectionListener(mTreeSelectionListener);
 239  0
                         mTree.updateUI();
 240  0
                         GlossItem item = (GlossItem) ((ArrayList<Object>) event
 241  
                                         .getOldValue()).get(1);
 242  0
                         selectTreePath(item);
 243  0
                         mTree.addTreeSelectionListener(mTreeSelectionListener);
 244  0
                 } else if (getAction(event, EActionCommands.GLOSS_ITEM_HEADING_CHANGED) || 
 245  
                                 getAction(event, EActionCommands.GLOSS_ITEM_PER_AUTO_UPDATE_CHANGED)) {
 246  0
                         mTree.removeTreeSelectionListener(mTreeSelectionListener);
 247  0
                         GlossItemTreeModel treeModel = null;
 248  0
                         if (mGlossItems.getFileName() != null) {
 249  0
                                 treeModel = new GlossItemTreeModel(mGlossItems,
 250  
                                                 mGlossItems.getFileName());
 251  
                         } else {
 252  0
                                 treeModel = new GlossItemTreeModel(mGlossItems,
 253  
                                                 mTranslation.getValue("tree.root.text"));
 254  
                         }
 255  0
                         mTree.setModel(treeModel);
 256  0
                         GlossItem item = (GlossItem) ((ArrayList<Object>) event
 257  
                                         .getOldValue()).get(1);
 258  0
                         selectTreePath(item);
 259  0
                         mSelectedItem = mGlossItems.get(item.getKey());
 260  0
                         mTree.updateUI();
 261  0
                         mTree.addTreeSelectionListener(mTreeSelectionListener);
 262  0
                 } else if (getAction(event, EActionCommands.FIRE_UNDO)) {
 263  0
                         mTree.updateUI();
 264  
                 } 
 265  0
         }
 266  
 
 267  
         private void insertGlossItem() {
 268  0
                 if (mSelectedItem != null) {
 269  0
                         mGlossItemsChangePublisher.addGlossItem(mSelectedItem.getHeading());
 270  
                 } else {
 271  0
                         Object node = null;
 272  0
                         if (mTree.getSelectionPath() != null) {
 273  0
                                 node = mTree.getSelectionPath().getLastPathComponent();
 274  
                         }
 275  0
                         if (node != null && node instanceof String) {
 276  0
                                 String heading = (String) node;
 277  0
                                 if (heading.equals(mGlossItems.getFileName()) == false
 278  
                                                 && heading.equals(mTranslation
 279  
                                                                 .getValue("tree.root.text")) == false) {
 280  0
                                         mGlossItemsChangePublisher.addGlossItem(heading);
 281  
                                 } else {
 282  0
                                         mGlossItemsChangePublisher.addGlossItem("");
 283  
                                 }
 284  0
                         } else {
 285  0
                                 mGlossItemsChangePublisher.addGlossItem("");
 286  
                         }
 287  
                 }
 288  0
         }
 289  
 
 290  
         private void removeGlossItem() {
 291  0
                 if (mSelectedItem != null) {
 292  0
                         mGlossItemsChangePublisher.removeGlossItem(mSelectedItem);
 293  0
                 } else if (mSelectedHeading != null) {
 294  0
                         mGlossItemsChangePublisher.removeGlossItemHeading(mSelectedHeading);
 295  
                 }
 296  0
         }
 297  
 
 298  
         @Override
 299  
         public void actionPerformed(ActionEvent event) {
 300  0
                 if (event.getActionCommand().equals(
 301  
                                 EActionCommands.ADD_GLOSS_ITEM.toString())) {
 302  0
                         insertGlossItem();
 303  0
                 } else if (event.getActionCommand().equals(
 304  
                                 EActionCommands.REMOVE_GLOSS_ITEM.toString())) {
 305  0
                         removeGlossItem();
 306  
                 }
 307  0
         }
 308  
 
 309  
         @Override
 310  
         public void keyPressed(KeyEvent e) {
 311  0
         }
 312  
 
 313  
         @Override
 314  
         public void keyReleased(KeyEvent e) {
 315  0
                 if (e.isControlDown() == true) {
 316  0
                         if (e.getKeyCode() == KeyEvent.VK_V) {
 317  0
                                 insertGlossItem();
 318  0
                         } else if (e.getKeyCode() == KeyEvent.VK_PLUS) {
 319  0
                                 expandAll(mTree, true);
 320  0
                         } else if (e.getKeyCode() == KeyEvent.VK_MINUS) {
 321  0
                                 expandAll(mTree, false);
 322  
                         }
 323  
                 }
 324  0
                 if (e.getKeyCode() == KeyEvent.VK_DELETE) {
 325  0
                         removeGlossItem();
 326  
                 }
 327  0
         }
 328  
 
 329  
         @Override
 330  
         public void keyTyped(KeyEvent e) {
 331  0
         }
 332  
 
 333  
         public void expandAll(JTree tree, boolean expand) {
 334  0
                 if (expand == true) {
 335  0
                         for (int i = 0; i < tree.getRowCount(); i++) {
 336  0
                                 tree.expandRow(i);
 337  
                         }
 338  
                 } else {
 339  0
                         for (int i = 1; i < tree.getRowCount(); i++) {
 340  0
                                 tree.collapseRow(i);
 341  
                         }
 342  
                 }
 343  0
         }
 344  
 
 345  
         @Override
 346  
         public void dragEnter(DropTargetDragEvent dtde) {
 347  
                 // TODO Auto-generated method stub
 348  
 
 349  0
         }
 350  
 
 351  
         @Override
 352  
         public void dragExit(DropTargetEvent dte) {
 353  
                 // TODO Auto-generated method stub
 354  
 
 355  0
         }
 356  
 
 357  
         @Override
 358  
         public void dragOver(DropTargetDragEvent dtde) {
 359  
                 // TODO Auto-generated method stub
 360  
 
 361  0
         }
 362  
 
 363  
         @SuppressWarnings("rawtypes")
 364  
         @Override
 365  
         // see <a href="http://www.java-tips.org/java-se-tips/javax.swing/how-to-implement-drag-drop-functionality-in-your-applic.html">Source</a>
 366  
         public void drop(DropTargetDropEvent dtde) {
 367  
                 try {
 368  0
                         boolean isRejected = true;
 369  0
                         Transferable tr = dtde.getTransferable();
 370  0
                         DataFlavor[] flavors = tr.getTransferDataFlavors();
 371  0
                         for (int i = 0; i < flavors.length; i++) {
 372  0
                                 if (flavors[i].isFlavorJavaFileListType()) {
 373  0
                                         dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
 374  0
                                         java.util.List list = (java.util.List) tr
 375  
                                                         .getTransferData(flavors[i]);
 376  0
                                         for (int j = 0; j < list.size(); j++) {
 377  0
                                                 System.out.println(list.get(j).toString() + "\n");
 378  
                                         }
 379  
                                         
 380  0
                                         if (list.size() > 0) {
 381  0
                                                 mGlossItemsChangePublisher.openFile(list.get(0).toString());
 382  
                                         }
 383  
 
 384  
                                         // If we made it this far, everything worked.
 385  0
                                         dtde.dropComplete(true);
 386  0
                                         isRejected = false;
 387  
                                 }
 388  
                         }
 389  0
                         if (isRejected) {
 390  0
                                 dtde.rejectDrop();
 391  
                         }
 392  0
                 } catch (Exception e) {
 393  0
                         e.printStackTrace();
 394  0
                         dtde.rejectDrop();
 395  0
                 }
 396  0
         }
 397  
 
 398  
         @Override
 399  
         public void dropActionChanged(DropTargetDragEvent dtde) {
 400  
                 // TODO Auto-generated method stub
 401  
 
 402  0
         }
 403  
 
 404  
 }