1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2006, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.mrep;
  28 
  29 import java.awt.BorderLayout;
  30 import java.awt.Color;
  31 import java.awt.Container;
  32 import java.awt.FlowLayout;
  33 import java.awt.GridLayout;
  34 import java.awt.event.ActionEvent;
  35 import java.awt.event.ActionListener;
  36 
  37 import javax.swing.BorderFactory;
  38 import javax.swing.Box;
  39 import javax.swing.DefaultListModel;
  40 import javax.swing.JButton;
  41 import javax.swing.JCheckBox;
  42 import javax.swing.JDialog;
  43 import javax.swing.JFrame;
  44 import javax.swing.JLabel;
  45 import javax.swing.JList;
  46 import javax.swing.JPanel;
  47 import javax.swing.JScrollPane;
  48 import javax.swing.ListSelectionModel;
  49 import javax.swing.border.Border;
  50 import javax.swing.event.ListSelectionEvent;
  51 import javax.swing.event.ListSelectionListener;
  52 
  53 import com.sun.javatest.tool.UIFactory;
  54 import javax.swing.JOptionPane;
  55 
  56 public class ConflictResolutionDialog extends JDialog {
  57 
  58     private String resolveButtonStr = "resolve";
  59     private String cancelButtonStr = "cancel";
  60     private String useMostRecentCheckBoxStr = "useMost";
  61 
  62     private JCheckBox preferredReportCheckBox;
  63     private JCheckBox useMostRecentCheckBox;
  64 
  65     private JButton resolveButton;
  66     private JButton cancelButton;
  67 
  68     private DefaultListModel<String> listModel;
  69     private JList list;
  70 
  71     private int     selectedIndex;
  72     private boolean bPreferredReport;
  73     private boolean bUseMostRecent;
  74 
  75     private UIFactory uif;
  76 
  77     private boolean cancel = false;
  78 
  79 
  80     public ConflictResolutionDialog(JFrame parent, String testName, String[] reportsList, boolean bPreferredSet, UIFactory uif) {
  81         super(parent, true);
  82         this.uif = uif;
  83 
  84         setName("conflict");
  85         setTitle(uif.getI18NString("conflict.name"));
  86         setResizable(false);
  87 
  88         Container cp = getContentPane();
  89         cp.setLayout(new BorderLayout());
  90         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  91         ConflictResolutionActionListener conflictResolutionListener = new ConflictResolutionActionListener();
  92 
  93         JLabel text = uif.createLabel("conflict.text");
  94         text.setText(text.getText() + " " + testName);
  95 
  96 
  97         text.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
  98         JPanel textPanel = uif.createPanel("conflict.text.panel", new FlowLayout(FlowLayout.CENTER));
  99         textPanel.add(text);
 100 
 101         Box vBox = Box.createVerticalBox();
 102 
 103         JLabel chooseText =uif.createLabel("conflict.chooseText");
 104         //text.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
 105         JPanel chooseTextPanel = uif.createPanel("conflict.choosePanel", new FlowLayout(FlowLayout.CENTER));
 106         chooseTextPanel.add(chooseText);
 107 
 108         // Build list box
 109         listModel=new DefaultListModel<>();
 110         for (int i=0; i< reportsList.length; i++) {
 111             listModel.addElement(reportsList[i]);
 112         }
 113         list = uif.createList("conflict.list", listModel);
 114         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 115 
 116         ReportsListSelectionListener rl = new ReportsListSelectionListener();
 117         list.addListSelectionListener(rl);
 118         Border brd = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK);
 119         list.setBorder(brd);
 120         JScrollPane scrollPane = uif.createScrollPane(list);
 121         Box hBox = Box.createHorizontalBox();
 122         //hBox.add(Box.createHorizontalStrut(20));
 123         hBox.add(scrollPane);
 124         //hBox.add(Box.createHorizontalStrut(20));
 125 
 126         preferredReportCheckBox = uif.createCheckBox("conflict.preffered");
 127         preferredReportCheckBox.setMnemonic(0);
 128         preferredReportCheckBox.setEnabled(false);
 129         JPanel preferredReportPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
 130         preferredReportPanel.add(preferredReportCheckBox);
 131 
 132 
 133         useMostRecentCheckBox = uif.createCheckBox("conflict.most.recent");
 134         useMostRecentCheckBox.setMnemonic(1);
 135         useMostRecentCheckBox.addActionListener(conflictResolutionListener);
 136         useMostRecentCheckBox.setActionCommand(useMostRecentCheckBoxStr);
 137         JPanel useRecentPanel = uif.createPanel("conflict.recent", new FlowLayout(FlowLayout.LEFT));
 138         useRecentPanel.add(useMostRecentCheckBox);
 139 
 140 
 141 
 142         vBox.setBorder(BorderFactory.createEmptyBorder(0,20,0,20));
 143         vBox.add(chooseTextPanel);
 144         vBox.add(hBox);
 145         // if preferred report was already chosen, in previous dialogs, it should not be seen here
 146         if (!bPreferredSet) {
 147             vBox.add(preferredReportPanel);
 148         }
 149         vBox.add(useRecentPanel);
 150 
 151 
 152         // Build control buttons
 153         JPanel controlButtonsPanel = uif.createPanel("conflict.control", new FlowLayout(FlowLayout.CENTER));
 154         JPanel p2 = new JPanel();
 155         p2.setLayout(new GridLayout(1,0,5,5));
 156 
 157         resolveButton = uif.createButton("conflict.resolve");
 158         resolveButton.setMnemonic(0);
 159         resolveButton.addActionListener(conflictResolutionListener);
 160         resolveButton.setActionCommand(resolveButtonStr);
 161         resolveButton.setEnabled(false);
 162 
 163         cancelButton = uif.createButton("conflict.cancel");
 164         cancelButton.addActionListener(conflictResolutionListener);
 165         cancelButton.setActionCommand(cancelButtonStr);
 166 
 167         p2.add(resolveButton);
 168         p2.add(cancelButton);
 169 
 170         controlButtonsPanel.add(p2);
 171         controlButtonsPanel.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));
 172 
 173         cp.add(textPanel, BorderLayout.NORTH);
 174         cp.add(vBox, BorderLayout.CENTER);
 175         cp.add(controlButtonsPanel, BorderLayout.SOUTH);
 176 
 177         pack();
 178         setLocationRelativeTo(parent);
 179     }
 180 
 181     public int  getSelectedIndex() {
 182         return selectedIndex;
 183     }
 184     public boolean getPreferredReport() {
 185         return bPreferredReport;
 186     }
 187 
 188     public boolean getUseMostRecent() {
 189         return bUseMostRecent;
 190     }
 191 
 192     public boolean wasCanceled() {
 193         return cancel;
 194     }
 195 
 196     class CancelException extends Exception {
 197 
 198     }
 199 
 200 
 201     class ConflictResolutionActionListener implements ActionListener {
 202 
 203         public void actionPerformed(ActionEvent e) {
 204 
 205             String cmd = e.getActionCommand();
 206             if (cmd.equals(cancelButtonStr)) {
 207 
 208                 if (uif.showYesNoDialog("conflict.areyousure") != JOptionPane.YES_OPTION)
 209                     return;
 210 
 211                 ConflictResolutionDialog.this.cancel = true;
 212                 ConflictResolutionDialog.this.dispose();
 213             } else if (cmd.equals(resolveButtonStr)) {
 214                 bUseMostRecent = useMostRecentCheckBox.isSelected();
 215                 bPreferredReport = preferredReportCheckBox.isSelected();
 216                 selectedIndex = list.getSelectedIndex();
 217                 ConflictResolutionDialog.this.dispose();
 218 
 219             } else if (cmd.equals(useMostRecentCheckBoxStr)) {
 220                 if ((list.getSelectedValues().length == 0) &&
 221                         (!useMostRecentCheckBox.isSelected())) {
 222                     resolveButton.setEnabled(false);
 223                 } else {
 224                     resolveButton.setEnabled(true);
 225                 }
 226 
 227 
 228                 if (useMostRecentCheckBox.isSelected()) {
 229                     list.setEnabled(false);
 230                     preferredReportCheckBox.setEnabled(false);
 231                 } else {
 232                     list.setEnabled(true);
 233                     preferredReportCheckBox.setEnabled(true);
 234                 }
 235             } else
 236                 ; // ignore events on all other objects
 237         }
 238     }
 239 
 240 
 241     class ReportsListSelectionListener implements ListSelectionListener {
 242         public void valueChanged(ListSelectionEvent e) {
 243             if(e.getValueIsAdjusting()) return;
 244 
 245             if ((list.getSelectedValues().length == 0) &&
 246                     (!useMostRecentCheckBox.isSelected())) {
 247                 resolveButton.setEnabled(false);
 248             } else {
 249                 resolveButton.setEnabled(true);
 250             }
 251 
 252 
 253             if (list.getSelectedValues().length == 0) {
 254                 preferredReportCheckBox.setEnabled(false);
 255             } else {
 256                 preferredReportCheckBox.setEnabled(true);
 257             }
 258 
 259         }
 260     }
 261 
 262 }