1208N/A/*
1208N/A * CDDL HEADER START
1208N/A *
1208N/A * The contents of this file are subject to the terms of the
1208N/A * Common Development and Distribution License, Version 1.0 only
1208N/A * (the "License"). You may not use this file except in compliance
1208N/A * with the License.
1208N/A *
1208N/A * You can obtain a copy of the license at
1208N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
1208N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
1208N/A * See the License for the specific language governing permissions
1208N/A * and limitations under the License.
1208N/A *
1208N/A * When distributing Covered Code, include this CDDL HEADER in each
1208N/A * file and include the License file at
1208N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
1208N/A * add the following below this CDDL HEADER, with the fields enclosed
1208N/A * by brackets "[]" replaced with your own identifying information:
1208N/A * Portions Copyright [yyyy] [name of copyright owner]
1208N/A *
1208N/A * CDDL HEADER END
1208N/A *
1208N/A *
5061N/A * Copyright 2008-2010 Sun Microsystems, Inc.
6238N/A * Portions Copyright 2013 ForgeRock AS.
1208N/A */
1208N/A
1208N/Apackage org.opends.quicksetup.installer.ui;
1208N/A
1208N/Aimport java.awt.Component;
1208N/Aimport java.awt.GridBagConstraints;
1208N/Aimport java.awt.GridBagLayout;
1208N/Aimport java.util.Comparator;
1208N/Aimport java.util.HashMap;
1208N/Aimport java.util.HashSet;
1208N/Aimport java.util.Set;
1208N/Aimport java.util.TreeSet;
1208N/A
1208N/Aimport javax.swing.Box;
1208N/Aimport javax.swing.JCheckBox;
1208N/Aimport javax.swing.JEditorPane;
1208N/Aimport javax.swing.JLabel;
1208N/Aimport javax.swing.JPanel;
1208N/Aimport javax.swing.JScrollPane;
1208N/Aimport javax.swing.SwingConstants;
1208N/A
1509N/Aimport org.opends.admin.ads.ADSContext;
1208N/Aimport org.opends.admin.ads.ReplicaDescriptor;
1208N/Aimport org.opends.admin.ads.ServerDescriptor;
1208N/Aimport org.opends.admin.ads.SuffixDescriptor;
1208N/A
2471N/Aimport org.opends.quicksetup.Constants;
1208N/Aimport org.opends.quicksetup.UserData;
2354N/Aimport org.opends.quicksetup.installer.AuthenticationData;
1208N/Aimport org.opends.quicksetup.installer.SuffixesToReplicateOptions;
1208N/Aimport org.opends.quicksetup.ui.FieldName;
1208N/Aimport org.opends.quicksetup.ui.GuiApplication;
1208N/Aimport org.opends.quicksetup.ui.QuickSetupStepPanel;
1208N/Aimport org.opends.quicksetup.ui.UIFactory;
1509N/Aimport org.opends.quicksetup.util.Utils;
1208N/A
2086N/Aimport org.opends.messages.Message;
2086N/Aimport org.opends.messages.MessageBuilder;
2086N/Aimport static org.opends.messages.QuickSetupMessages.*;
2086N/A
1208N/A/**
1208N/A * This class is used to provide a data model for the list of suffixes that
1208N/A * we have to replicate on the new server.
1208N/A */
1208N/Apublic class SuffixesToReplicatePanel extends QuickSetupStepPanel
1208N/Aimplements Comparator<SuffixDescriptor>
1208N/A{
1208N/A private static final long serialVersionUID = -8051367953737385327L;
1208N/A private TreeSet<SuffixDescriptor> orderedSuffixes =
1208N/A new TreeSet<SuffixDescriptor>(this);
1509N/A private HashMap<String, JCheckBox> hmCheckBoxes =
1509N/A new HashMap<String, JCheckBox>();
2354N/A // The display of the server the user provided in the replication options
2354N/A // panel
2354N/A private String serverToConnectDisplay = null;
2354N/A
1208N/A private JLabel noSuffixLabel;
1208N/A private Component labelGlue;
1208N/A private JPanel checkBoxPanel;
1208N/A private JScrollPane scroll;
1208N/A
1208N/A /**
1208N/A * Constructor of the panel.
1208N/A * @param application Application represented by this panel and used to
1208N/A * initialize the fields of the panel.
1208N/A */
1208N/A public SuffixesToReplicatePanel(GuiApplication application)
1208N/A {
1208N/A super(application);
1208N/A createComponents();
1208N/A }
1208N/A
1208N/A /**
1208N/A * {@inheritDoc}
1208N/A */
1208N/A public Object getFieldValue(FieldName fieldName)
1208N/A {
1208N/A Object value = null;
1208N/A
1208N/A if (fieldName == FieldName.SUFFIXES_TO_REPLICATE_OPTIONS)
1208N/A {
4533N/A value = SuffixesToReplicateOptions.Type.REPLICATE_WITH_EXISTING_SUFFIXES;
1208N/A }
1208N/A else if (fieldName == FieldName.SUFFIXES_TO_REPLICATE)
1208N/A {
1208N/A Set<SuffixDescriptor> suffixes = new HashSet<SuffixDescriptor>();
1208N/A for (SuffixDescriptor suffix:orderedSuffixes)
1208N/A {
1509N/A if (hmCheckBoxes.get(suffix.getId()).isSelected())
1208N/A {
1208N/A suffixes.add(suffix);
1208N/A }
1208N/A }
1208N/A value = suffixes;
1208N/A }
1208N/A
1208N/A return value;
1208N/A }
1208N/A
1208N/A /**
1208N/A * {@inheritDoc}
1208N/A */
1208N/A public int compare(SuffixDescriptor desc1, SuffixDescriptor desc2)
1208N/A {
6238N/A int result = compareSuffixDN(desc1, desc2);
1208N/A if (result == 0)
1208N/A {
1208N/A result = compareSuffixStrings(desc1, desc2);
1208N/A }
1208N/A return result;
1208N/A }
1208N/A
1208N/A /**
1208N/A * {@inheritDoc}
1208N/A */
1208N/A protected Component createInputPanel()
1208N/A {
1208N/A JPanel panel = new JPanel(new GridBagLayout());
1208N/A panel.setOpaque(false);
1208N/A
1208N/A GridBagConstraints gbc = new GridBagConstraints();
1208N/A gbc.weightx = 1.0;
1208N/A gbc.anchor = GridBagConstraints.NORTHWEST;
1208N/A gbc.fill = GridBagConstraints.HORIZONTAL;
1208N/A gbc.gridwidth = GridBagConstraints.REMAINDER;
1208N/A gbc.insets = UIFactory.getEmptyInsets();
1208N/A
1208N/A gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
1208N/A gbc.insets.left = UIFactory.LEFT_INSET_SUBPANEL_SUBORDINATE;
1208N/A
1208N/A // Add the checkboxes
1208N/A checkBoxPanel = new JPanel(new GridBagLayout());
1208N/A checkBoxPanel.setOpaque(false);
1208N/A gbc.insets.top = 0;
1208N/A gbc.anchor = GridBagConstraints.NORTH;
1208N/A gbc.weighty = 1.0;
1208N/A gbc.fill = GridBagConstraints.BOTH;
4972N/A scroll = UIFactory.createBorderLessScrollBar(checkBoxPanel);
1208N/A
1208N/A panel.add(scroll, gbc);
1208N/A
1208N/A gbc.weighty = 0.0;
1208N/A gbc.fill = GridBagConstraints.HORIZONTAL;
1208N/A gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
1208N/A gbc.anchor = GridBagConstraints.NORTHEAST;
1208N/A panel.add(noSuffixLabel, gbc);
1208N/A noSuffixLabel.setVisible(false);
1208N/A
1208N/A labelGlue = Box.createVerticalGlue();
1208N/A gbc.fill = GridBagConstraints.VERTICAL;
1208N/A gbc.weighty = 1.0;
1208N/A panel.add(labelGlue, gbc);
1208N/A labelGlue.setVisible(false);
1208N/A
1208N/A return panel;
1208N/A }
1208N/A
1208N/A /**
1208N/A * {@inheritDoc}
1208N/A */
4972N/A protected boolean requiresScroll()
4972N/A {
4972N/A return false;
4972N/A }
4972N/A
4972N/A /**
4972N/A * {@inheritDoc}
4972N/A */
2086N/A protected Message getInstructions()
1208N/A {
2086N/A return INFO_SUFFIXES_TO_REPLICATE_PANEL_INSTRUCTIONS.get();
1208N/A }
1208N/A
1208N/A /**
1208N/A * {@inheritDoc}
1208N/A */
2086N/A protected Message getTitle()
1208N/A {
2086N/A return INFO_SUFFIXES_TO_REPLICATE_PANEL_TITLE.get();
1208N/A }
1208N/A
1208N/A /**
1208N/A * {@inheritDoc}
1208N/A */
1208N/A public void beginDisplay(UserData data)
1208N/A {
1208N/A TreeSet<SuffixDescriptor> array = orderSuffixes(
1208N/A data.getSuffixesToReplicateOptions().getAvailableSuffixes());
1509N/A
2354N/A AuthenticationData authData =
2354N/A data.getReplicationOptions().getAuthenticationData();
2354N/A String newServerDisplay;
3802N/A if (authData != null)
1208N/A {
2354N/A newServerDisplay = authData.getHostName()+":"+authData.getPort();
2354N/A }
2354N/A else
2354N/A {
2354N/A newServerDisplay = "";
2354N/A }
2354N/A if (!array.equals(orderedSuffixes) ||
2354N/A !newServerDisplay.equals(serverToConnectDisplay))
2354N/A {
2354N/A serverToConnectDisplay = newServerDisplay;
1509N/A HashMap<String, Boolean> hmOldValues = new HashMap<String, Boolean>();
1509N/A for (String id : hmCheckBoxes.keySet())
1509N/A {
1509N/A hmOldValues.put(id, hmCheckBoxes.get(id).isSelected());
1509N/A }
1208N/A orderedSuffixes.clear();
1509N/A for (SuffixDescriptor suffix : array)
1509N/A {
1509N/A if (!Utils.areDnsEqual(suffix.getDN(),
2471N/A ADSContext.getAdministrationSuffixDN()) &&
2471N/A !Utils.areDnsEqual(suffix.getDN(), Constants.SCHEMA_DN) &&
2471N/A !Utils.areDnsEqual(suffix.getDN(),
2471N/A Constants.REPLICATION_CHANGES_DN))
1509N/A {
1509N/A orderedSuffixes.add(suffix);
1509N/A }
1509N/A }
1208N/A hmCheckBoxes.clear();
1208N/A for (SuffixDescriptor suffix : orderedSuffixes)
1208N/A {
2086N/A JCheckBox cb = UIFactory.makeJCheckBox(Message.raw(suffix.getDN()),
2086N/A INFO_SUFFIXES_TO_REPLICATE_DN_TOOLTIP.get(),
1208N/A UIFactory.TextStyle.SECONDARY_FIELD_VALID);
1208N/A cb.setOpaque(false);
1509N/A Boolean v = hmOldValues.get(suffix.getId());
1509N/A if (v != null)
1509N/A {
1509N/A cb.setSelected(v);
1509N/A }
1509N/A hmCheckBoxes.put(suffix.getId(), cb);
1208N/A }
1208N/A populateCheckBoxPanel();
1509N/A }
1509N/A boolean display = orderedSuffixes.size() > 0;
1208N/A
1509N/A noSuffixLabel.setVisible(!display);
1509N/A labelGlue.setVisible(!display);
1509N/A scroll.setVisible(display);
1208N/A }
1208N/A
1208N/A /**
1208N/A * Creates the components of this panel.
1208N/A */
1208N/A private void createComponents()
1208N/A {
1208N/A noSuffixLabel = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
2086N/A INFO_SUFFIX_LIST_EMPTY.get(),
1208N/A UIFactory.TextStyle.SECONDARY_FIELD_VALID);
1208N/A }
1208N/A
1208N/A private void populateCheckBoxPanel()
1208N/A {
1208N/A checkBoxPanel.removeAll();
1208N/A GridBagConstraints gbc = new GridBagConstraints();
5061N/A gbc.gridy = 0;
1208N/A gbc.fill = GridBagConstraints.BOTH;
1208N/A gbc.anchor = GridBagConstraints.NORTH;
1208N/A boolean first = true;
1208N/A for (SuffixDescriptor suffix : orderedSuffixes)
1208N/A {
1208N/A gbc.insets.left = 0;
1208N/A gbc.weightx = 0.0;
1208N/A if (!first)
1208N/A {
1208N/A gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
1208N/A }
1208N/A gbc.gridwidth = GridBagConstraints.RELATIVE;
1509N/A JCheckBox cb = hmCheckBoxes.get(suffix.getId());
1208N/A cb.setVerticalAlignment(SwingConstants.TOP);
5061N/A gbc.gridx = 0;
1208N/A checkBoxPanel.add(cb, gbc);
1208N/A gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD;
1208N/A gbc.weightx = 1.0;
1208N/A gbc.gridwidth = GridBagConstraints.REMAINDER;
2086N/A JEditorPane l = UIFactory.makeTextPane(
2086N/A Message.raw(getSuffixString(suffix)),
2086N/A UIFactory.TextStyle.SECONDARY_FIELD_VALID);
1208N/A l.setOpaque(false);
1208N/A
1208N/A /* Use a prototype label to get the additional insets */
2086N/A JEditorPane proto = UIFactory.makeTextPane(
2086N/A Message.raw(suffix.getDN()),
1208N/A UIFactory.TextStyle.SECONDARY_FIELD_VALID);
1208N/A
1208N/A gbc.insets.top += Math.abs(cb.getPreferredSize().height -
1208N/A proto.getPreferredSize().height) / 2;
5061N/A gbc.gridx = 1;
1208N/A checkBoxPanel.add(l, gbc);
1208N/A first = false;
5061N/A gbc.gridy ++;
1208N/A }
5061N/A gbc.weighty = 1.0;
5061N/A gbc.insets = UIFactory.getEmptyInsets();
5061N/A gbc.fill = GridBagConstraints.VERTICAL;
5061N/A checkBoxPanel.add(Box.createVerticalGlue(), gbc);
1208N/A }
1208N/A
1208N/A private String getSuffixString(SuffixDescriptor desc)
1208N/A {
2086N/A TreeSet<Message> replicaDisplays = new TreeSet<Message>();
1208N/A for (ReplicaDescriptor rep: desc.getReplicas())
1208N/A {
1208N/A replicaDisplays.add(getReplicaDisplay(rep));
1208N/A }
2086N/A MessageBuilder buf = new MessageBuilder();
2086N/A for (Message display: replicaDisplays)
1208N/A {
1208N/A if (buf.length() > 0)
1208N/A {
1208N/A buf.append("\n");
1208N/A }
1208N/A buf.append(display);
1208N/A }
1208N/A return buf.toString();
1208N/A }
1208N/A
2086N/A private Message getReplicaDisplay(ReplicaDescriptor replica)
1208N/A {
2086N/A Message display;
1208N/A
1208N/A ServerDescriptor server = replica.getServer();
1208N/A
2354N/A String serverDisplay;
2354N/A if (server.getHostPort(false).equalsIgnoreCase(serverToConnectDisplay))
2354N/A {
2354N/A serverDisplay = serverToConnectDisplay;
2354N/A }
2354N/A else
2354N/A {
2354N/A serverDisplay = server.getHostPort(true);
2354N/A }
1208N/A
1208N/A int nEntries = replica.getEntries();
1208N/A
1208N/A if (nEntries > 0)
1208N/A {
2086N/A display = INFO_SUFFIX_LIST_REPLICA_DISPLAY_ENTRIES.get(
2086N/A serverDisplay, String.valueOf(nEntries));
1208N/A }
1509N/A else if (nEntries == 0)
1509N/A {
2086N/A display = INFO_SUFFIX_LIST_REPLICA_DISPLAY_NO_ENTRIES.get(serverDisplay);
1509N/A }
1208N/A else
1208N/A {
2086N/A display = INFO_SUFFIX_LIST_REPLICA_DISPLAY_ENTRIES_NOT_AVAILABLE.get(
2086N/A serverDisplay);
1208N/A }
1208N/A
1208N/A return display;
1208N/A }
1208N/A
1208N/A private TreeSet<SuffixDescriptor> orderSuffixes(
1509N/A Set<SuffixDescriptor> suffixes)
1208N/A {
1208N/A TreeSet<SuffixDescriptor> ordered = new TreeSet<SuffixDescriptor>(this);
1208N/A ordered.addAll(suffixes);
1208N/A
1208N/A return ordered;
1208N/A }
1208N/A
1208N/A private int compareSuffixDN(SuffixDescriptor desc1, SuffixDescriptor desc2)
1208N/A {
1208N/A return desc1.getDN().compareTo(desc2.getDN());
1208N/A }
1208N/A
1208N/A private int compareSuffixStrings(SuffixDescriptor desc1,
1208N/A SuffixDescriptor desc2)
1208N/A {
1208N/A return getSuffixString(desc1).compareTo(getSuffixString(desc2));
1208N/A }
1208N/A}
1208N/A