src/core/net/sf/basedb/util/overview/node/NameableNameGenerator.java

Code
Comments
Other
Rev Date Author Line
4740 05 Feb 09 nicklas 1 /**
4740 05 Feb 09 nicklas 2   $Id$
4740 05 Feb 09 nicklas 3
4740 05 Feb 09 nicklas 4   Copyright (C) 2009 Nicklas Nordborg
4740 05 Feb 09 nicklas 5
4740 05 Feb 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
4740 05 Feb 09 nicklas 7   Available at http://base.thep.lu.se/
4740 05 Feb 09 nicklas 8
4740 05 Feb 09 nicklas 9   BASE is free software; you can redistribute it and/or
4740 05 Feb 09 nicklas 10   modify it under the terms of the GNU General Public License
4740 05 Feb 09 nicklas 11   as published by the Free Software Foundation; either version 3
4740 05 Feb 09 nicklas 12   of the License, or (at your option) any later version.
4740 05 Feb 09 nicklas 13
4740 05 Feb 09 nicklas 14   BASE is distributed in the hope that it will be useful,
4740 05 Feb 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4740 05 Feb 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4740 05 Feb 09 nicklas 17   GNU General Public License for more details.
4740 05 Feb 09 nicklas 18
4740 05 Feb 09 nicklas 19   You should have received a copy of the GNU General Public License
4740 05 Feb 09 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4740 05 Feb 09 nicklas 21 */
4740 05 Feb 09 nicklas 22 package net.sf.basedb.util.overview.node;
4740 05 Feb 09 nicklas 23
4740 05 Feb 09 nicklas 24 import net.sf.basedb.core.Nameable;
4740 05 Feb 09 nicklas 25 import net.sf.basedb.util.overview.Node;
4740 05 Feb 09 nicklas 26
4740 05 Feb 09 nicklas 27 /**
4740 05 Feb 09 nicklas 28   Name generator implementation for {@link Nameable}
4740 05 Feb 09 nicklas 29   items. The name and title can each have two variants,
4740 05 Feb 09 nicklas 30   depending on the node type of the parent node.
4740 05 Feb 09 nicklas 31   If the parent node is a folder-type node, the name
4740 05 Feb 09 nicklas 32   is generated as <code>prefix.item-id</code> and the
4740 05 Feb 09 nicklas 33   title is the name of the item, eg. {@link Nameable#getName()}.
4740 05 Feb 09 nicklas 34   <p>
4740 05 Feb 09 nicklas 35   If the parent node is an item-type node, the name
4740 05 Feb 09 nicklas 36   is only the namePrefix and the title is the title 
4740 05 Feb 09 nicklas 37   prefix combined with the name of the item.
4740 05 Feb 09 nicklas 38   
4740 05 Feb 09 nicklas 39   @author Nicklas
4740 05 Feb 09 nicklas 40   @version 2.10
4740 05 Feb 09 nicklas 41   @base.modified $Date$
4740 05 Feb 09 nicklas 42 */
4740 05 Feb 09 nicklas 43 public class NameableNameGenerator<I extends Nameable>
4740 05 Feb 09 nicklas 44   implements NodeNameGenerator<I>
4740 05 Feb 09 nicklas 45 {
4740 05 Feb 09 nicklas 46
4740 05 Feb 09 nicklas 47   private String namePrefix;
4740 05 Feb 09 nicklas 48   private String titlePrefix;
4740 05 Feb 09 nicklas 49   
4740 05 Feb 09 nicklas 50   /**
4740 05 Feb 09 nicklas 51     Create a new name generator.
4740 05 Feb 09 nicklas 52     @param namePrefix The prefix to use in node names
4740 05 Feb 09 nicklas 53     @param titlePrefix The prefix to use in node titles
4740 05 Feb 09 nicklas 54   */
4740 05 Feb 09 nicklas 55   public NameableNameGenerator(String namePrefix, String titlePrefix)
4740 05 Feb 09 nicklas 56   {
4740 05 Feb 09 nicklas 57     this.namePrefix = namePrefix;
4740 05 Feb 09 nicklas 58     this.titlePrefix = titlePrefix;
4740 05 Feb 09 nicklas 59   }
4740 05 Feb 09 nicklas 60   
4740 05 Feb 09 nicklas 61   @Override
4740 05 Feb 09 nicklas 62   public String getNodeName(I item, Node parentNode)
4740 05 Feb 09 nicklas 63   {
4740 05 Feb 09 nicklas 64     StringBuilder name = new StringBuilder(namePrefix);
4740 05 Feb 09 nicklas 65     if (parentNode != null && parentNode.getNodeType() == Node.Type.FOLDER)
4740 05 Feb 09 nicklas 66     {
4740 05 Feb 09 nicklas 67       name.append(".").append(item.getId());
4740 05 Feb 09 nicklas 68     }
4740 05 Feb 09 nicklas 69     return name.toString();
4740 05 Feb 09 nicklas 70   }
4740 05 Feb 09 nicklas 71
4740 05 Feb 09 nicklas 72   @Override
4740 05 Feb 09 nicklas 73   public String getNodeTitle(I item, Node parentNode)
4740 05 Feb 09 nicklas 74   {
4740 05 Feb 09 nicklas 75     StringBuilder title = new StringBuilder();
5651 08 Jun 11 nicklas 76     if (parentNode != null && parentNode.getNodeType() == Node.Type.ITEM)
4740 05 Feb 09 nicklas 77     {
4749 11 Feb 09 nicklas 78       if (titlePrefix != null) title.append(titlePrefix).append(": ");
4740 05 Feb 09 nicklas 79     }
4740 05 Feb 09 nicklas 80     title.append(item.getName());
4740 05 Feb 09 nicklas 81     return title.toString();
4740 05 Feb 09 nicklas 82   }
4740 05 Feb 09 nicklas 83
4740 05 Feb 09 nicklas 84   @Override
4740 05 Feb 09 nicklas 85   public String getDeniedNodeName(Node parentNode)
4740 05 Feb 09 nicklas 86   {
4740 05 Feb 09 nicklas 87     return namePrefix;
4740 05 Feb 09 nicklas 88   }
4740 05 Feb 09 nicklas 89
4740 05 Feb 09 nicklas 90   @Override
4740 05 Feb 09 nicklas 91   public String getDeniedNodeTitle(Node parentNode)
4740 05 Feb 09 nicklas 92   {
4740 05 Feb 09 nicklas 93     return titlePrefix + ": denied";
4740 05 Feb 09 nicklas 94   }
4740 05 Feb 09 nicklas 95
4740 05 Feb 09 nicklas 96   @Override
4740 05 Feb 09 nicklas 97   public String getMissingNodeName(Node parentNode)
4740 05 Feb 09 nicklas 98   {
4740 05 Feb 09 nicklas 99     return namePrefix;
4740 05 Feb 09 nicklas 100   }
4740 05 Feb 09 nicklas 101
4740 05 Feb 09 nicklas 102   @Override
4740 05 Feb 09 nicklas 103   public String getMissingNodeTitle(Node parentNode)
4740 05 Feb 09 nicklas 104   {
4740 05 Feb 09 nicklas 105     return titlePrefix + ": missing";
4740 05 Feb 09 nicklas 106   }
4740 05 Feb 09 nicklas 107
4740 05 Feb 09 nicklas 108 }