001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2008-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2015-2016 ForgeRock AS. 016 */ 017package org.opends.guitools.controlpanel.ui; 018 019import static org.opends.server.util.StaticUtils.*; 020 021import java.util.HashSet; 022import java.util.LinkedHashSet; 023import java.util.Set; 024 025import javax.swing.JList; 026import javax.swing.border.Border; 027import javax.swing.border.EmptyBorder; 028 029import org.forgerock.opendj.ldap.schema.AttributeType; 030import org.forgerock.opendj.ldap.schema.ObjectClass; 031import org.opends.guitools.controlpanel.event.SchemaElementSelectionEvent; 032import org.opends.guitools.controlpanel.event.SchemaElementSelectionListener; 033import org.opends.server.types.Schema; 034 035/** 036 * Abstract class used to re-factor some code among the panels that display the 037 * contents of a schema element. 038 */ 039public abstract class SchemaElementPanel extends StatusGenericPanel 040{ 041 private static final long serialVersionUID = -8556383593966382604L; 042 043 private Set<SchemaElementSelectionListener> listeners = new HashSet<>(); 044 045 /** 046 * The empty border shared by all the schema element panels. 047 */ 048 protected Border PANEL_BORDER = new EmptyBorder(10, 10, 10, 10); 049 050 /** 051 * Adds a schema element selection listener. 052 * @param listener the listener. 053 */ 054 public void addSchemaElementSelectionListener( 055 SchemaElementSelectionListener listener) 056 { 057 listeners.add(listener); 058 } 059 060 /** 061 * Removes a schema element selection listener. 062 * @param listener the listener. 063 */ 064 public void removeSchemaElementSelectionListener( 065 SchemaElementSelectionListener listener) 066 { 067 listeners.add(listener); 068 } 069 070 /** 071 * Notifies to all the listeners that a new schema element was selected. 072 * @param schemaElement the new schema element that has been selected. 073 */ 074 protected void notifySchemaSelectionListeners(Object schemaElement) 075 { 076 for (SchemaElementSelectionListener listener : listeners) 077 { 078 listener.schemaElementSelected( 079 new SchemaElementSelectionEvent(this, schemaElement)); 080 } 081 } 082 083 /** 084 * Method used to know if there are unsaved changes or not. It is used by 085 * the schema selection listener when the user changes the selection. 086 * @return <CODE>true</CODE> if there are unsaved changes (and so the 087 * selection of the schema should be canceled) and <CODE>false</CODE> 088 * otherwise. 089 */ 090 public boolean mustCheckUnsavedChanges() 091 { 092 return false; 093 } 094 095 /** 096 * Tells whether the user chose to save the changes in the panel, to not save 097 * them or simply cancelled the selection in the tree. 098 * @return the value telling whether the user chose to save the changes in the 099 * panel, to not save them or simply cancelled the selection in the tree. 100 */ 101 public UnsavedChangesDialog.Result checkUnsavedChanges() 102 { 103 return UnsavedChangesDialog.Result.DO_NOT_SAVE; 104 } 105 106 /** 107 * Method called when there is an object class selected in a list. 108 * @param list the list. 109 */ 110 protected void objectClassSelected(JList<?> list) 111 { 112 String o = (String)list.getSelectedValue(); 113 Schema schema = getInfo().getServerDescriptor().getSchema(); 114 if (o != null && schema != null) 115 { 116 ObjectClass oc = schema.getObjectClass(o); 117 if (!oc.isPlaceHolder()) 118 { 119 notifySchemaSelectionListeners(oc); 120 } 121 } 122 } 123 124 /** 125 * Returns the list of aliases for the provided attribute. 126 * @param attr the attribute. 127 * @return the list of aliases for the provided attribute. 128 */ 129 protected Set<String> getAliases(AttributeType attr) 130 { 131 return getAliases(attr.getNames(), attr.getNameOrOID()); 132 } 133 134 /** 135 * Returns the list of aliases for the provided object class. 136 * @param oc the object class. 137 * @return the list of aliases for the provided object class. 138 */ 139 protected Set<String> getAliases(ObjectClass oc) 140 { 141 return getAliases(oc.getNames(), oc.getNameOrOID()); 142 } 143 144 private Set<String> getAliases(Iterable<String> names, String nameOrOid) 145 { 146 nameOrOid = nameOrOid != null ? nameOrOid : ""; 147 148 final Set<String> aliases = new LinkedHashSet<>(); 149 for (String name : names) 150 { 151 if (!name.equalsIgnoreCase(nameOrOid)) 152 { 153 aliases.add(toLowerCase(name)); 154 } 155 } 156 return aliases; 157 } 158}