Author: dandre Date: Fri Oct 4 23:30:51 2013 New Revision: 4595 Log: * more dialog box refactoring
Modified: trunk/AncestrisCore/core/src/ancestris/util/swing/DialogManager.java trunk/AncestrisCore/core/src/genj/util/swing/DialogHelper.java trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditNote.java trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditSource.java trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/GedcomDialog.java Modified: trunk/AncestrisCore/core/src/ancestris/util/swing/DialogManager.java ============================================================================== --- trunk/AncestrisCore/core/src/ancestris/util/swing/DialogManager.java (original) +++ trunk/AncestrisCore/core/src/ancestris/util/swing/DialogManager.java Fri Oct 4 23:30:51 2013 @@ -1,6 +1,13 @@ /* - * To change this template, choose Tools | Templates - * and open the template in the editor. + * Ancestris - http://www.ancestris.org + * + * Copyright 2012-2013 Ancestris + * + * Author: Daniel Andre ([email protected]). + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ //XXX: we must redesign this class and DialogManager in a more NB integrated manner package ancestris.util.swing; @@ -22,20 +29,20 @@ * * @author daniel */ +//FIXME:: write doc and default values for options ans message type public abstract class DialogManager { /** message types */ - public static final int - ERROR_MESSAGE = NotifyDescriptor.ERROR_MESSAGE, + public static final int ERROR_MESSAGE = NotifyDescriptor.ERROR_MESSAGE, INFORMATION_MESSAGE = NotifyDescriptor.INFORMATION_MESSAGE, WARNING_MESSAGE = NotifyDescriptor.WARNING_MESSAGE, QUESTION_MESSAGE = NotifyDescriptor.QUESTION_MESSAGE, PLAIN_MESSAGE = NotifyDescriptor.PLAIN_MESSAGE; - public static final int - OK_CANCEL_OPTION = DialogDescriptor.OK_CANCEL_OPTION, + public static final int OK_CANCEL_OPTION = DialogDescriptor.OK_CANCEL_OPTION, YES_NO_OPTION = NotifyDescriptor.YES_NO_OPTION, YES_NO_CANCEL_OPTION = NotifyDescriptor.YES_NO_CANCEL_OPTION, - OK_ONLY_OPTION = 10;; + OK_ONLY_OPTION = 10; + ; /** Return value if OK is chosen. */ public static final Object OK_OPTION = DialogDescriptor.OK_OPTION; @@ -57,7 +64,7 @@ } public static ADialog create(String title, JComponent[] content) { - // assemble content into Box (don't use Box here because + // assemble content into Box (don't use Box here because // Box extends Container in pre JDK 1.4) JPanel box = new JPanel(); box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS)); @@ -71,15 +78,16 @@ } return create(title, box); } - + public static InputLine create(String title, String text, String value) { return new InputLine(title, text, value); } /** * Creates a simple error dialog message with only an ok button. + * * @param title - * @param text +param text * * @return */ @@ -95,15 +103,14 @@ /** * Creates a simple question dialog message with only a yes and no button. * Message type defaults to QUESTION_MESSAGE + * * @param title - * @param text +param text * * @return */ public static DialogManager createYesNo(String title, String text) { - return new Message(title, text) - .setOptionType(NotifyDescriptor.YES_NO_OPTION) - .setMessageType(QUESTION_MESSAGE); + return new Message(title, text).setOptionType(NotifyDescriptor.YES_NO_OPTION).setMessageType(QUESTION_MESSAGE); } // see http://wiki.netbeans.org/DevFaqDialogControlOKButton @@ -174,8 +181,8 @@ } public DialogManager setOptionType(int newType) { - if (newType == OK_ONLY_OPTION){ - return setOptions(new Object [] {OK_OPTION}); + if (newType == OK_ONLY_OPTION) { + return setOptions(new Object[]{OK_OPTION}); } else { getDescriptor().setOptions(null); getDescriptor().setOptionType(newType); @@ -255,6 +262,7 @@ public static class ADialog extends DialogManager { protected DialogDescriptor descriptor; + private Dialog dialog; public ADialog(String title, JComponent content) { super(); @@ -267,7 +275,7 @@ } public Object show() { - final Dialog dialog = DialogDisplayer.getDefault().createDialog(descriptor); + dialog = DialogDisplayer.getDefault().createDialog(descriptor); // restore bounds if (dialogId != null) { final Registry registry = Registry.get(DialogManager.class); @@ -284,5 +292,35 @@ } return descriptor.getValue(); } + + public void cancel() { + if (dialog == null) { + throw new IllegalStateException("not showing"); + } + dialog.dispose(); + } } + //FIXME: from old DialogHelper. See if this is necessary + /** + * scan for JTabbedPanes and make their contained components opaque + */ +// private static void patchOpaque(Component component, boolean set) { +// +// if (component instanceof JTabbedPane) { +// set = false; +// } +// +// if (component instanceof JComponent && !(component instanceof JTextField) && !(component instanceof JScrollPane)) { +// if (!set) { +// ((JComponent) component).setOpaque(set); +// } +// } +// +// if (component instanceof Container && !(component instanceof JScrollPane)) { +// for (Component c : ((Container) component).getComponents()) { +// patchOpaque(c, set); +// } +// } +// +// } } Modified: trunk/AncestrisCore/core/src/genj/util/swing/DialogHelper.java ============================================================================== --- trunk/AncestrisCore/core/src/genj/util/swing/DialogHelper.java (original) +++ trunk/AncestrisCore/core/src/genj/util/swing/DialogHelper.java Fri Oct 4 23:30:51 2013 @@ -1,52 +1,23 @@ -/** - * GenJ - GenealogyJ - * - * Copyright (C) 1997 - 2002 Nils Meier <[email protected]> +/* + * Ancestris - http://www.ancestris.org * - * This piece of code is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. + * Copyright 2012-2013 Ancestris * - * This code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * Author: Daniel Andre ([email protected]). * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ package genj.util.swing; -import genj.util.Registry; -import java.awt.BorderLayout; import java.awt.Component; -import java.awt.Container; -import java.awt.Dimension; -import java.awt.HeadlessException; -import java.awt.Rectangle; -import java.awt.Toolkit; import java.awt.Window; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; import java.util.EventObject; -import javax.swing.Action; -import javax.swing.JButton; -import javax.swing.JComponent; import javax.swing.JDialog; -import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.JPanel; import javax.swing.JPopupMenu; -import javax.swing.JScrollPane; -import javax.swing.JTabbedPane; -import javax.swing.JTextField; -import org.openide.NotifyDescriptor; /** * Helper for interacting with Dialogs and Windows @@ -59,10 +30,6 @@ @Deprecated public class DialogHelper { - /** screen we're dealing with */ - private final static Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); - /** message types */ - public static final int QUESTION_MESSAGE = NotifyDescriptor.QUESTION_MESSAGE; public static Window getWindow(EventObject event) { if (!(event.getSource() instanceof Component)) { @@ -79,194 +46,6 @@ }); } - // FIXME: This class is deprecated and used only in GedcomEditor. - @Deprecated - public static class Dialog { - - private String title; - private int messageType; - private final JComponent content; - private Action[] actions; - private Component parent; - private JDialog dlg; - - public Dialog(String title, int messageType, final JComponent content, Action[] actions, Object source) { - - this.title = title; - this.messageType = messageType; - this.content = content; - this.actions = actions; - - // find window for source - parent = null; - if (source instanceof Component) { - parent = (Component) source; - } else if (source instanceof EventObject && ((EventObject) source).getSource() instanceof Component) { - parent = visitOwners((Component) ((EventObject) source).getSource(), new ComponentVisitor() { - - public Component visit(Component parent, Component child) { - return parent == null ? child : null; - } - }); - } - - // patch opaqueness of content - patchOpaque(content, true); - - // done for now - } - - public int show() { - - // create an option pane - final JOptionPane optionPane = new Content(messageType, content, actions); - - // create the dialog and content - dlg = optionPane.createDialog(parent, title); - dlg.setResizable(true); - dlg.setModal(true); - dlg.pack(); - dlg.setMinimumSize(content.getMinimumSize()); - - // restore bounds - StackTraceElement caller = getCaller(); - final Registry registry = Registry.get(caller.getClassName()); -// pour le moment on ne sauvegarde pas les dimensions des dialog box - final String key = caller.getMethodName() + (caller.getLineNumber() > 0 ? caller.getLineNumber() : "") + ".dialog"; -// Dimension bounds = null; //registry.get(key, (Dimension)null); - Dimension bounds = registry.get(key, (Dimension) null); - if (bounds != null) { - bounds.width = Math.max(bounds.width, dlg.getWidth()); - bounds.height = Math.max(bounds.height, dlg.getHeight()); - dlg.setBounds(new Rectangle(bounds).intersection(screen)); - } - dlg.setLocationRelativeTo(parent); - - // hook up to the dialog being hidden by the optionpane - that's what is being called after the user selected a button (setValue()) - dlg.addComponentListener(new ComponentAdapter() { - - public void componentHidden(ComponentEvent e) { - registry.put(key, dlg.getSize()); - dlg.dispose(); - dlg.removeComponentListener(this); - dlg = null; - } - }); - - // show it - dlg.setVisible(true); - - // analyze - check which action was responsible for close - Object rc = optionPane.getValue(); - for (int a = 0; a < actions.length; a++) { - if (rc == actions[a]) { - return a; - } - } - return -1; - } - - public void cancel() { - if (dlg == null) { - throw new IllegalStateException("not showing"); - } - dlg.dispose(); - } - } - - private static StackTraceElement getCaller() { - String clazz = DialogHelper.class.getName(); - for (StackTraceElement element : new Throwable().getStackTrace()) { - if (!clazz.equals(element.getClassName())) { - return element; - } - } - // shouldn't happen - return new StackTraceElement("Class", "method", "file", 0); - } - - /** - * A patched up JOptionPane - */ - private static class Content extends JOptionPane { - - private JDialog dlg; - private JComponent content; - - /** constructor */ - private Content(int messageType, JComponent content, Action[] actions) { - super(new JLabel(), messageType, JOptionPane.DEFAULT_OPTION, null, new String[0]); - - this.content = content; - - // wrap content in a JPanel - the OptionPaneUI has some code that - // depends on this to stretch it :( - JPanel wrapper = new JPanel(new BorderLayout()); - wrapper.add(BorderLayout.CENTER, content); - setMessage(wrapper); - - // create our action buttons - Option[] options = new Option[actions.length]; - for (int i = 0; i < actions.length; i++) { - options[i] = new Option(actions[i]); - } - setOptions(options); - - // set defalut? - if (options.length > 0) { - setInitialValue(options[0]); - } - - // done - } - - @Override - public JDialog createDialog(Component parentComponent, String title) throws HeadlessException { - dlg = super.createDialog(parentComponent, title); - return dlg; - } - - public void doLayout() { - - super.doLayout(); - - // check min size on dialog - if (dlg != null) { - Dimension c = getSize(); - Dimension m = getMinimumSize(); - - Dimension size = dlg.getSize(); - boolean set = false; - if ((set |= m.width > c.width)) { - size.width += m.width - c.width; - } - if ((set |= m.height > c.height)) { - size.height += m.height - c.height; - } - - if (set) { - dlg.setSize(size); - } - } - } - - /** an option in our option-pane */ - private class Option extends JButton implements ActionListener { - - /** constructor */ - private Option(Action action) { - super(action); - addActionListener(this); - } - - /** trigger */ - public void actionPerformed(ActionEvent e) { - // this will actually force the dialog to hide - JOptionPane listens to property changes - setValue(getAction()); - } - } //Action2Button - } // Content - /** * Visit containers of a component recursively. This method follows the getParent() * hierarchy. @@ -339,47 +118,4 @@ public Component visit(Component component, Component child); } - /** - * scan for JTabbedPanes and make their contained components opaque - */ - private static void patchOpaque(Component component, boolean set) { - - if (component instanceof JTabbedPane) { - set = false; - } - - if (component instanceof JComponent && !(component instanceof JTextField) && !(component instanceof JScrollPane)) { - if (!set) { - ((JComponent) component).setOpaque(set); - } - } - - if (component instanceof Container && !(component instanceof JScrollPane)) { - for (Component c : ((Container) component).getComponents()) { - patchOpaque(c, set); - } - } - - } - - /** - * Check containment - * - * @param component component to look for - * @param container container to look in - * - * @return true if component.getParent()*==container - */ - public static boolean isContained(Component component, final Container container) { - return container == visitContainers(component, new ComponentVisitor() { - - @Override - public Component visit(Component parent, Component child) { - if (parent == container) { - return parent; - } - return null; - } - }); - } } //AbstractWindowManager Modified: trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditNote.java ============================================================================== --- trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditNote.java (original) +++ trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditNote.java Fri Oct 4 23:30:51 2013 @@ -32,7 +32,7 @@ import genj.gedcom.UnitOfWork; import genj.util.Resources; import ancestris.core.actions.AbstractAncestrisAction; -import genj.util.swing.DialogHelper; +import ancestris.util.swing.DialogManager; import genj.util.swing.ImageIcon; import genj.util.swing.NestedBlockLayout; @@ -116,7 +116,13 @@ else if (note!=null) text.setText(note.getValue()); - if (0!=new GedcomDialog(property.getGedcom(), property.toString() + " - " + getTip(), DialogHelper.QUESTION_MESSAGE, panel, AbstractAncestrisAction.okCancel(), e).show()) + if (new GedcomDialog( + property.getGedcom(), + property.toString() + " - " + getTip(), + panel) + .setMessageType(DialogManager.QUESTION_MESSAGE) + .setOptionType(DialogManager.OK_CANCEL_OPTION) + .show() != DialogManager.OK_OPTION) return; property.getGedcom().doMuteUnitOfWork(new UnitOfWork() { Modified: trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditSource.java ============================================================================== --- trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditSource.java (original) +++ trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/EditSource.java Fri Oct 4 23:30:51 2013 @@ -32,6 +32,7 @@ import genj.gedcom.UnitOfWork; import genj.util.Resources; import ancestris.core.actions.AbstractAncestrisAction; +import ancestris.util.swing.DialogManager; import genj.util.swing.DialogHelper; import genj.util.swing.ImageIcon; import genj.util.swing.NestedBlockLayout; @@ -100,30 +101,40 @@ if (!sources.isEmpty()) { final TableWidget<PropertySource> table = new TableWidget<PropertySource>(); - AbstractAncestrisAction[] actions = new AbstractAncestrisAction[]{ AbstractAncestrisAction.ok(), new AbstractAncestrisAction(RESOURCES.getString("link", Gedcom.getName("SOUR"))) }; - final GedcomDialog dlg = new GedcomDialog(property.getGedcom(), property.toString() + " - " + getTip(), DialogHelper.QUESTION_MESSAGE, new JScrollPane(table), actions, e); + String linkSource = RESOURCES.getString("link", Gedcom.getName("SOUR")); + final GedcomDialog dlg = new GedcomDialog( + property.getGedcom(), + property.toString() + " - " + getTip(), + new JScrollPane(table)); + dlg.setMessageType(DialogManager.QUESTION_MESSAGE); + dlg.setOptions(new Object[]{DialogManager.OK_OPTION,linkSource}); table.new Column(Gedcom.getName("SOUR")) { + @Override public Object getValue(PropertySource source) { return source.getTargetEntity().getId(); } }; table.new Column(Gedcom.getName("AUTH")) { + @Override public Object getValue(PropertySource source) { return source.getTargetEntity().getPropertyDisplayValue("AUTH"); } }; table.new Column(Gedcom.getName("TITL")) { + @Override public Object getValue(PropertySource source) { return source.getTargetEntity().getPropertyDisplayValue("TITL"); } }; table.new Column(Gedcom.getName("PAGE")) { + @Override public Object getValue(PropertySource source) { return source.getPropertyDisplayValue("PAGE"); } }; table.new Column("", AbstractAncestrisAction.class) { + @Override public Object getValue(PropertySource source) { return new Edit(source,false) { @Override @@ -147,7 +158,7 @@ // }; table.setRows(sources); - if (dlg.show()<1) + if (dlg.show()!=linkSource) return; } @@ -199,9 +210,12 @@ sourcePanel.setRoot(citation.getTargetEntity()); tabs.add(Gedcom.getName("SOUR"), sourcePanel); - GedcomDialog dlg = new GedcomDialog(citation.getGedcom(), getText(), DialogHelper.QUESTION_MESSAGE, tabs, AbstractAncestrisAction.okCancel(), e); - if (0==dlg.show()) + GedcomDialog dlg = new GedcomDialog(citation.getGedcom(), getText(), tabs); + dlg.setMessageType(DialogManager.QUESTION_MESSAGE); + dlg.setOptionType(DialogManager.OK_CANCEL_OPTION); + if (dlg.show() == DialogManager.OK_OPTION) citation.getGedcom().doMuteUnitOfWork(new UnitOfWork() { + @Override public void perform(Gedcom gedcom) throws GedcomException { sourcePanel.commit(); citationPanel.commit(); @@ -210,6 +224,7 @@ else if (deleteOnCancel) citation.getGedcom().doMuteUnitOfWork(new UnitOfWork() { + @Override public void perform(Gedcom gedcom) throws GedcomException { Source source = (Source)citation.getTargetEntity(); citation.getParent().delProperty(citation); Modified: trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/GedcomDialog.java ============================================================================== --- trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/GedcomDialog.java (original) +++ trunk/AncestrisCore/modules.editors.gedcom/src/genj/edit/actions/GedcomDialog.java Fri Oct 4 23:30:51 2013 @@ -1,57 +1,49 @@ -/** - * GenJ - GenealogyJ - * - * Copyright (C) 1997 - 2010 Nils Meier <[email protected]> +/* + * Ancestris - http://www.ancestris.org * - * This piece of code is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. + * Copyright 2012-2013 Ancestris * - * This code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * Author: Daniel Andre ([email protected]). * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ package genj.edit.actions; +import ancestris.util.swing.DialogManager; import genj.gedcom.Gedcom; import genj.gedcom.GedcomListener; import genj.gedcom.GedcomListenerAdapter; -import genj.util.swing.DialogHelper; - -import javax.swing.Action; import javax.swing.JComponent; /** * Editing dialog for a gedcom context that auto-dismisses on edit */ -public class GedcomDialog extends DialogHelper.Dialog { - - private Gedcom gedcom; - private GedcomListener listener = new GedcomListenerAdapter() { - public void gedcomWriteLockAcquired(Gedcom gedcom) { - cancel(); - } - }; - - public GedcomDialog(Gedcom gedcom, String title, int messageType, final JComponent content, Action[] actions, Object source) { - super(title, messageType, content, actions, source); - this.gedcom = gedcom; - } - - @Override - public int show() { - try { - gedcom.addGedcomListener(listener); - return super.show(); - } finally { - gedcom.removeGedcomListener(listener); +public class GedcomDialog extends DialogManager.ADialog { + + private Gedcom gedcom; + private GedcomListener listener = new GedcomListenerAdapter() { + + @Override + public void gedcomWriteLockAcquired(Gedcom gedcom) { + cancel(); + } + }; + + public GedcomDialog(Gedcom gedcom, String title, final JComponent content) { + super(title, content); + this.gedcom = gedcom; } - } + @Override + public Object show() { + try { + gedcom.addGedcomListener(listener); + return super.show(); + } finally { + gedcom.removeGedcomListener(listener); + } + + } } \ No newline at end of file --------------------------------------------------------------------- Site Web Ancestris : http://www.ancestris.org <*> Pour vous desinscrire de cette liste, envoyez un mail a : [email protected] <*> Pour obtenir de l'aide sur les commandes de la liste : [email protected] Pour obtenir tous les messages lies a ce fil de discussion, cliquez sur le lien ci-dessous, cela ouvrira votre logiciel de messagerie. Il vous suffira d'envoyer le message : [email protected]
