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