001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.event;
021    
022    /*
023     * Created 14.02.2003 13:40:19.
024     * $Id: DefaultTreeActionListener.java 1368577 2012-08-02 16:20:31Z lofwyr $
025     */
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.apache.myfaces.tobago.component.UITreeOld;
030    import org.apache.myfaces.tobago.context.ResourceManagerUtil;
031    import org.apache.myfaces.tobago.model.TreeState;
032    
033    import javax.faces.component.UIComponent;
034    import javax.faces.component.UIPanel;
035    import javax.faces.context.FacesContext;
036    import javax.faces.event.AbortProcessingException;
037    import javax.faces.event.ActionEvent;
038    import javax.faces.event.ActionListener;
039    import javax.swing.tree.DefaultMutableTreeNode;
040    import javax.swing.tree.MutableTreeNode;
041    
042    public class DefaultTreeActionListener implements ActionListener {
043    
044      private static final Log LOG = LogFactory.getLog(DefaultTreeActionListener.class);
045    
046      protected DefaultMutableTreeNode create(FacesContext facesContext) {
047        String label = ResourceManagerUtil.getPropertyNotNull(facesContext, "tobago", "treeNodeNew");
048        return new DefaultMutableTreeNode(label);
049      }
050    
051      protected DefaultMutableTreeNode copy(DefaultMutableTreeNode node) {
052        return new DefaultMutableTreeNode(node.getUserObject());
053      }
054    
055      public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
056    
057        FacesContext facesContext = FacesContext.getCurrentInstance();
058        UIComponent component = actionEvent.getComponent().getParent();
059        if (component instanceof UIPanel) {
060          // component is toolbar
061          component = component.getParent();
062        }
063        if (!(component instanceof UITreeOld)) {
064          LOG.error("No tree found!");
065          return;
066        }
067    
068        UITreeOld tree = (UITreeOld) component;
069        TreeState treeState = tree.getState();
070        DefaultMutableTreeNode marker = treeState.getMarker();
071        String command = actionEvent.getComponent().getId();
072    
073        if (LOG.isDebugEnabled()) {
074          LOG.debug("marker      " + marker);
075          LOG.debug("lastMarker  " + treeState.getLastMarker());
076          LOG.debug("root        " + tree.getValue());
077          LOG.debug("command     " + command);
078          LOG.debug("lastCommand " + treeState.getLastCommand());
079        }
080        if (marker != null) {
081          boolean isRoot = tree.getValue().equals(marker);
082          if (UITreeOld.COMMAND_NEW.equals(command)) {
083            treeState.commandNew(create(facesContext));
084          } else if (UITreeOld.COMMAND_DELETE.equals(command)) {
085            if (!isRoot) {
086              marker.removeFromParent();
087            }
088            treeState.setLastMarker(null);
089            treeState.setLastCommand(null);
090          } else if (UITreeOld.COMMAND_CUT.equals(command)) {
091            if (!isRoot) {
092              treeState.setLastMarker(marker);
093              treeState.setLastCommand(command);
094            }
095          } else if (UITreeOld.COMMAND_COPY.equals(command)) {
096            treeState.setLastMarker(marker);
097            treeState.setLastCommand(command);
098          } else if (UITreeOld.COMMAND_PASTE.equals(command)) {
099            if (treeState.getLastMarker() != null) {
100              if (UITreeOld.COMMAND_CUT.equals(treeState.getLastCommand())) {
101                marker.insert(treeState.getLastMarker(), 0);
102              } else if (UITreeOld.COMMAND_COPY.equals(treeState.getLastCommand())) {
103                marker.insert(copy(treeState.getLastMarker()), 0);
104              }
105              treeState.setLastMarker(null);
106              treeState.setLastCommand(null);
107            }
108          } else if (UITreeOld.COMMAND_MOVE_UP.equals(command)) {
109            if (!isRoot) {
110              MutableTreeNode node = marker;
111              MutableTreeNode parent = (MutableTreeNode) node.getParent();
112              int index = parent.getIndex(node);
113              index = Math.max(index - 1, 0);
114              parent.insert(node, index);
115            }
116            treeState.setLastMarker(null);
117            treeState.setLastCommand(null);
118          } else if (UITreeOld.COMMAND_MOVE_DOWN.equals(command)) {
119            if (!isRoot) {
120              MutableTreeNode node = marker;
121              MutableTreeNode parent = (MutableTreeNode) node.getParent();
122              int index = parent.getIndex(node);
123              index = Math.min(index + 1, parent.getChildCount() - 1);
124              parent.insert(node, index);
125            }
126            treeState.setLastMarker(null);
127            treeState.setLastCommand(null);
128          }
129        }
130      }
131    }