RichEdit control classes - swing menus, dialogs, panels and frames.

X-SVN-Rev: 1187
This commit is contained in:
Alan Liu 2000-04-20 17:46:22 +00:00
parent 4df0579885
commit 3e71bec515
8 changed files with 1141 additions and 0 deletions

View file

@ -0,0 +1,70 @@
/*
* @(#)$RCSfile: JMessageDialog.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import java.awt.Color;
import java.awt.Container;
import java.awt.CardLayout;
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import com.ibm.richtext.styledtext.MConstText;
import com.ibm.richtext.textpanel.JTextPanel;
import com.ibm.richtext.textpanel.TextPanelSettings;
/**
* MessageDialog is a simple Frame which displays a styled
* text message in a TextPanel.
* The text in the message is not selectable or editable.
* @see MConstText
* @see JTextPanel
*/
public final class JMessageDialog extends JFrame {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
/**
* Create a new MessageDialog.
* @param title the dialog's title
* @param message the text which will appear in the dialog
*/
public JMessageDialog(String title, MConstText message)
{
super(title);
Container content = getContentPane();
content.setLayout(new CardLayout());
TextPanelSettings settings = JTextPanel.getDefaultSettings();
settings.setScrollable(false);
settings.setSelectable(false);
JTextPanel panel = new JTextPanel(settings, message, null);
panel.setBackground(Color.black);
content.add("Center", panel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
}
});
setSize(450,320);
}
}

View file

@ -0,0 +1,162 @@
/*
* @(#)$RCSfile: JNumberDialog.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import com.ibm.richtext.uiimpl.resources.FrameResources;
import com.ibm.richtext.uiimpl.MenuItemSet;
import com.ibm.richtext.uiimpl.ResourceUtils;
import com.ibm.richtext.styledtext.StyleModifier;
import com.ibm.richtext.textpanel.MTextPanel;
/**
* Simple dialog which gets a number, and sends an appropriate command
*/
final class JNumberDialog extends JDialog implements ActionListener
{
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private MTextPanel fTextPanel;
private JTextField fInput = null;
private JButton fOKButton = null;
private JButton fCancelButton = null;
private boolean fCharacter;
private Object fKey;
private float fMultiplier;
/**
* @param multiplier the factor by which to multiply the user's
* selection before creating the attribute value. This
* is useful for subscripting.
*/
JNumberDialog(Frame parent,
String title,
String message,
MTextPanel textPanel,
Object key,
boolean character,
float multiplier) {
super(parent, title, false);
fTextPanel = textPanel;
fKey = key;
fCharacter = character;
fMultiplier = multiplier;
Container content = getContentPane();
content.setLayout(new java.awt.GridLayout(2,1));
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15));
fInput = new JTextField(5);
panel.add(new JLabel(message));
panel.add(fInput);
content.add("Center", panel);
fCancelButton = new JButton(ResourceUtils.getResourceString(FrameResources.CANCEL));
fOKButton = new JButton(ResourceUtils.getResourceString(FrameResources.OK));
JPanel p = new JPanel();
p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
p.add(fCancelButton);
p.add(fOKButton);
content.add("South", p);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeWindow(false);
}
});
fOKButton.addActionListener(this);
fCancelButton.addActionListener(this);
}
private void closeWindow(boolean sendAction) {
setVisible(false);
int num = 0;
if (sendAction) {
try {
String text = fInput.getText();
num = NumberFormat.getInstance().parse(text).intValue();
}
catch (ParseException exception) {
sendAction = false;
}
}
if (sendAction) {
sendAction(num);
}
dispose();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == fOKButton) {
closeWindow(true);
}
else if (source == fCancelButton) {
closeWindow(false);
}
else {
throw new IllegalArgumentException("Invalid ActionEvent!");
}
}
/**
* Handle the user input
* @param the number the user typed in
*/
private void sendAction(int number) {
float num = number * fMultiplier;
StyleModifier modifier = StyleModifier.createAddModifier(
fKey,
new Float(num));
if (fCharacter == MenuItemSet.CHARACTER) {
fTextPanel.modifyCharacterStyleOnSelection(modifier);
}
else {
fTextPanel.modifyParagraphStyleOnSelection(modifier);
}
}
}

View file

@ -0,0 +1,280 @@
/*
* @(#)$RCSfile: JObjectDialog.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import java.util.Hashtable;
import javax.swing.Box;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import com.ibm.textlayout.attributes.AttributeMap;
import com.ibm.textlayout.attributes.AttributeSet;
import com.ibm.richtext.styledtext.MConstText;
import com.ibm.richtext.styledtext.StyleModifier;
import com.ibm.richtext.textpanel.MTextPanel;
import com.ibm.richtext.uiimpl.MenuItemSet;
import com.ibm.richtext.uiimpl.resources.FrameResources;
import com.ibm.richtext.uiimpl.ResourceUtils;
/**
* Simple dialog that sets an attribute.
*/
final class JObjectDialog extends JDialog {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private final MTextPanel fTextPanel;
private final Object fKey;
private boolean fCharacter;
private Hashtable fNameToValueMap;
private final JButton fOKButton;
private final JButton fCancelButton;
private final JList fItems;
/**
* Construct a new JObjectDialog.
* @param parent the dialog's parent frame
* @param title the dialogs title
* @param message the message displayed next to the input box
*/
JObjectDialog(Frame parent,
String title,
String message,
MTextPanel textPanel,
Object key,
boolean character,
String[] names,
Object[] values) {
super(parent, title, false);
setupMap(names, values);
Dimension size = new Dimension(250, 200);
fTextPanel = textPanel;
fKey = key;
fCharacter = character;
fItems = new JList(names);
fItems.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
fItems.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
closeWindow(true);
}
}
});
JScrollPane listScroller = new JScrollPane(fItems);
listScroller.setPreferredSize(size);
listScroller.setPreferredSize(size);
listScroller.setAlignmentX(LEFT_ALIGNMENT);
JLabel label = new JLabel(message);
label.setLabelFor(fItems);
JPanel itemPanel = new JPanel();
itemPanel.add(label);
itemPanel.add(Box.createRigidArea(new Dimension(0,5)));
itemPanel.add(listScroller);
itemPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.Y_AXIS));
fCancelButton = new JButton(ResourceUtils.getResourceString(FrameResources.CANCEL));
fOKButton = new JButton(ResourceUtils.getResourceString(FrameResources.OK));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
buttonPanel.add(fCancelButton);
buttonPanel.add(fOKButton);
Container content = getContentPane();
content.add(itemPanel, BorderLayout.CENTER);
content.add(buttonPanel, BorderLayout.SOUTH);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeWindow(false);
}
});
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == fOKButton) {
closeWindow(true);
}
else if (source == fCancelButton) {
closeWindow(false);
}
}
};
fOKButton.addActionListener(listener);
fCancelButton.addActionListener(listener);
selectStyles(values);
}
private void setupMap(String[] names, Object[] values) {
if (names.length != values.length) {
throw new IllegalArgumentException("Must have same number of names and values.");
}
fNameToValueMap = new Hashtable(names.length);
for (int i=0; i < names.length; i++) {
if (values[i] != null) {
fNameToValueMap.put(names[i], values[i]);
}
}
}
private void closeWindow(boolean sendAction) {
setVisible(false);
if (sendAction && fItems.getMinSelectionIndex() != fItems.getMaxSelectionIndex()) {
sendAction = false;
}
if (sendAction) {
Object value = fNameToValueMap.get(fItems.getSelectedValue());
sendAction(value);
}
dispose();
}
/**
* Handle the user input
* @param obj the value object
*/
private void sendAction(Object value) {
StyleModifier modifier;
if (value != null) {
modifier = StyleModifier.createAddModifier(fKey, value);
}
else {
AttributeSet set = new AttributeSet(fKey);
modifier = StyleModifier.createRemoveModifier(set);
}
if (fCharacter == MenuItemSet.CHARACTER) {
fTextPanel.modifyCharacterStyleOnSelection(modifier);
}
else {
fTextPanel.modifyParagraphStyleOnSelection(modifier);
}
}
private void selectValue(Object value, Object[] values) {
for (int i=0; i < values.length; i++) {
if ((value != null && value.equals(values[i])) || (value == null && values[i] == null)) {
fItems.addSelectionInterval(i, i);
fItems.ensureIndexIsVisible(i);
return;
}
}
}
private void selectStyles(Object[] values) {
Object value;
if (fCharacter) {
value = fTextPanel.getCharacterStyleOverSelection(fKey);
}
else {
value = fTextPanel.getParagraphStyleOverSelection(fKey);
}
if (value != MTextPanel.MULTIPLE_VALUES) {
selectValue(value, values);
}
else {
fOKButton.setEnabled(false);
int selLimit = fTextPanel.getSelectionEnd();
MConstText text = fTextPanel.getText();
for (int runStart = fTextPanel.getSelectionStart(); runStart <= selLimit;
runStart = fCharacter? text.characterStyleLimit(runStart) :
text.paragraphLimit(runStart)) {
Object runVal;
if (fCharacter) {
runVal = text.characterStyleAt(runStart).get(fKey);
}
else {
runVal = text.paragraphStyleAt(runStart).get(fKey);
}
if (runVal == null) {
runVal = fTextPanel.getDefaultValues().get(fKey);
}
selectValue(runVal, values);
if (runStart == text.length()) {
break;
}
}
}
fItems.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
fItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fOKButton.setEnabled(true);
fItems.removeListSelectionListener(this);
}
});
}
}

View file

@ -0,0 +1,134 @@
/*
* @(#)$RCSfile: JTabRuler.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import com.ibm.richtext.textpanel.MTextPanel;
import com.ibm.richtext.styledtext.MTabRuler;
import com.ibm.richtext.uiimpl.TabRulerImpl;
import com.ibm.richtext.awtui.MTabRulerComponent;
/**
* JTabRuler is an implementation of MTabRulerComponent in a Swing component.
*/
public final class JTabRuler extends JComponent implements MTabRulerComponent {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private TabRulerImpl fImpl;
/**
* Create a new TabRuler.
* @param baseline the y-coordinate of the ruler's baseline
* @param origin the x-coordinate in this Component where
* the left margin appears
* @param textPanel the MTextPanel to listen to. This TabRuler
* will reflect the MTextPanel's paragraph styles, and update
* the paragraph styles when manipulated.
*/
public JTabRuler(int baseline, int origin, MTextPanel textPanel) {
fImpl = new TabRulerImpl(baseline, origin, textPanel, this);
}
/**
* Listen to the given MTextPanel and reflect its changes,
* and update its paragraph styles when TabRuler is
* manipulated.
* @param textPanel the MTextPanel to listen to
*/
public void listenToTextPanel(MTextPanel textPanel) {
fImpl.listenToTextPanel(textPanel);
}
/**
* Return the background color of this TabRuler.
* @return the background color of this TabRuler
*/
public Color getBackColor() {
return fImpl.getBackColor();
}
/**
* Set the background color of this TabRuler.
* @param backColor the new background color of this TabRuler
*/
public void setBackColor(Color backColor) {
fImpl.setBackColor(backColor);
}
/**
* Return the MTabRuler represented by this TabRuler.
* @return the MTabRuler represented by this TabRuler
*/
public MTabRuler getRuler() {
return fImpl.getRuler();
}
/**
* Return the leading margin of this TabRuler.
* @return the leading margin of this TabRuler
*/
public int getLeadingMargin() {
return fImpl.getLeadingMargin();
}
/**
* Return the first line indent of this TabRuler.
* @return the first line indent of this TabRuler
*/
public int getFirstLineIndent() {
return fImpl.getFirstLineIndent();
}
/**
* Return the trailing margin of this TabRuler.
* @return the trailing margin of this TabRuler
*/
public final int getTrailingMargin() {
return fImpl.getTrailingMargin();
}
// The following are Component methods which need to be delegated to
// the implementation:
public void paint(Graphics g) {
fImpl.paint(g);
}
public Dimension getPreferredSize() {
return fImpl.getPreferredSize();
}
public Dimension getMinimumSize() {
return fImpl.getMinimumSize();
}
}

View file

@ -0,0 +1,146 @@
/*
* @(#)$RCSfile: JTextFrame.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import com.ibm.richtext.textpanel.MTextPanel;
import com.ibm.richtext.textpanel.JTextPanel;
import com.ibm.richtext.styledtext.MConstText;
import com.ibm.richtext.uiimpl.resources.FrameResources;
import com.ibm.richtext.uiimpl.ResourceUtils;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
import java.awt.Container;
/**
* JTextFrame is a JFrame containing an editable JTextPanel, a set of standard
* menus, and a JTabRuler. This class can be used as-is, but is
* primarily intended to be a simple example of how to use the other classes
* in this package.
* @see com.ibm.richtext.textpanel.JTextPanel
* @see SwingMenuBuilder
* @see JTabRuler
*/
public final class JTextFrame extends JFrame {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private JTextPanel fTextPanel;
/**
* Create a new JTextFrame with no text, no title,
* and a private clipboard.
*/
public JTextFrame() {
super();
init(null, Toolkit.getDefaultToolkit().getSystemClipboard());
}
/**
* Create a new JTextFrame with no text and the given title.
* The JTextPanel will use a private clipboard.
* @param title the title of this Frame
*/
public JTextFrame(String title) {
super(title);
init(null, Toolkit.getDefaultToolkit().getSystemClipboard());
}
/**
* Create a new JTextFrame with the given text and title, whose
* TextPanel will use the given clipboard.
* @param text the initial text in the TextPanel. If null the
* TextPanel will initially be empty
* @param title the title of this Frame
* @param clipboard the Clipboard which the TextPanel will use.
* If null the TextPanel will use a private Clipboard
*/
public JTextFrame(MConstText text,
String title,
Clipboard clipboard) {
super(title);
init(text, clipboard);
}
private void init(MConstText text, Clipboard clipboard) {
fTextPanel = new JTextPanel(text, clipboard);
JTabRuler tabRuler = new JTabRuler(14, 10, fTextPanel);
createMenus();
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(fTextPanel, "Center");
contentPane.add(tabRuler, "North");
pack();
}
private void createMenus() {
JMenuBar menuBar = new JMenuBar();
SwingMenuBuilder.getInstance().createMenus(menuBar, fTextPanel, this);
setJMenuBar(menuBar);
}
/**
* Return the MTextPanel in this frame.
*/
public MTextPanel getTextPanel() {
return fTextPanel;
}
public static void main(String[] args) {
String laf = UIManager.getSystemLookAndFeelClassName();
if (args.length == 1) {
if (args[0].equals("cp")) {
laf = UIManager.getCrossPlatformLookAndFeelClassName();
}
}
try {
UIManager.setLookAndFeel(laf);
}
catch(Throwable th) {
th.printStackTrace();
}
JTextFrame frame = new JTextFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(550, 700);
frame.show();
}
}

View file

@ -0,0 +1,236 @@
/*
* @(#)$RCSfile: SwingMenuBuilder.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import java.awt.Frame;
import java.awt.Window;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import com.ibm.richtext.textpanel.MTextPanel;
import com.ibm.richtext.uiimpl.resources.FrameResources;
import com.ibm.richtext.uiimpl.*;
import com.ibm.richtext.uiimpl.DialogItem.DialogFactory;
/**
* SwingMenuBuilder provides a method for creating a set of Swing menus for interacting
* with an MTextPanel. Future versions of this class may provide greater control
* over the menu contents.
* @see MTextPanel
*/
public final class SwingMenuBuilder extends MenuBuilder {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private static final SwingMenuBuilder INSTANCE = new SwingMenuBuilder();
/**
* Id for an Edit menu. The Edit menu has the following items:
* <ul>
* <li><b>Undo</b> - invoke undo() on the MTextPanel</li>
* <li><b>Redo</b> - invoke redo() on the MTextPanel</li>
* <li><b>Cut</b> - invoke cut() on the MTextPanel</li>
* <li><b>Copy</b> - invoke copy() on the MTextPanel</li>
* <li><b>Paste</b> - invoke paste() on the MTextPanel</li>
* <li><b>Clear</b> - invoke clear() on the MTextPanel</li>
* <li><b>Select All</b> - invoke selectAll() on the MTextPanel</li>
* </ul>
*/
public static final int EDIT = MenuBuilder.EDIT;
/**
* Id for the point sizes menu. The menu has items that set the size of a character
* in a typeface.
*/
public static final int SIZE = MenuBuilder.SIZE;
/**
* Id for a Style menu. The Style menu has the following items:
* <ul>
* <li><b>Plain</b> - remove <code>WEIGHT</code>,
* <code>POSTURE</code>,
* <code>UNDERLINE</code> and
* <code>STRIKETHROUGH</code> attributes from the
* current selection</li>
* <li><b>Bold</b> - add <code>{WEIGHT,WEIGHT_BOLD}</code> to
* the current selection</li>
* <li><b>Italic</b> - add <code>{POSTURE,POSTURE_ITALIC}</code> to
* the current selection</li>
* <li><b>Underline</b> - add <code>{UNDERLINE,UNDERLINE_ON}</code> to
* the current selection</li>
* <li><b>Strikethrough</b> - add <code>{STRIKETHROUGH,STRIKETHROUGH_ON}</code>
* to the current selection</li>
* <li><b>Font...</b> - display a dialog allowing the user to
* select a typeface (font family) for the current selection</li>
* <li><b>Forecolor...</b> - display a dialog allowing the user to
* select a foreground color for the current selection</li>
* <li><b>Backcolor...</b> - display a dialog allowing the user to
* select a background color for the current selection</li>
* </ul>
*/
public static final int STYLE = MenuBuilder.STYLE;
/**
* Id for a paragraph alignment menu. The menu has the following items:
* <ul>
* <li><b>Leading</b> - give selected paragraph(s) LEADING flush</li>
* <li><b>Center</b> - give selected paragraph(s) CENTER flush</li>
* <li><b>Trailing</b> - give selected paragraph(s) TRAILING flush</li>
* <li><b>Justified</b> - give selected paragraph(s) full justification</li>
* </ul>
*/
public static final int FLUSH = MenuBuilder.FLUSH;
/**
* Id for a menu that sets the KeyRemap
* on an MTextPanel. The menu has the following items:
* <ul>
* <li><b>Default</b> - set KeyRemap to identity remap</li>
* <li><b>Arabic</b> - set KeyRemap to Arabic transliteration</li>
* <li><b>Hebrew</b> - set KeyRemap to Hebrew transliteration</li>
* <li><b>Israel Nikud</b> - set KeyRemap to Israel Nikud</li>
* <li><b>Thai Ketmanee</b> - set KeyRemap to Thai Ketmanee</li>
* </ul>
*/
public static final int KEYMAP = MenuBuilder.KEYMAP;
/**
* Id for a menu that sets
* the primary run direction for a paragraph. Run direction can be left-to-right,
* right-to-left, or can use the default run direction from the Unicode bidi algorithm.
*/
public static final int BIDI = MenuBuilder.BIDI;
/**
* Id for a menu with an <b>About</b> item. When selected,
* the item displays a Frame containing some
* self-promotional text.
*/
public static final int ABOUT = MenuBuilder.ABOUT;
/**
* Return an instance of SwingMenuBuilder.
*/
public static SwingMenuBuilder getInstance() {
return INSTANCE;
}
private JMenuBar fMenuBar;
private SwingMenuBuilder() {
}
/**
* Add a standard set of menus to the given menu bar. The menus
* will interact with the given MTextPanel.
* @param menuBar the MenuBar to which menus are added
* @param textPanel the MTextPanel with which the menus interact
* @param frame a Frame to use as the parent of any dialogs created by a
* a menu item. If null, menu items which create dialogs will be omitted.
*/
public void createMenus(JMenuBar menuBar,
MTextPanel textPanel,
Frame frame) {
createMenus(menuBar, textPanel, frame, defaultMenus);
}
/**
* Add a set of menus to the given menu bar. The menus
* will interact with the given MTextPanel.
* @param menuBar the MenuBar to which menus are added
* @param textPanel the MTextPanel with which the menus interact
* @param frame a Frame to use as the parent of any dialogs created by a
* a menu item. If null, menu items which create dialogs will be omitted.
* @param menus an array of integer menu id's. Each element of the
* array must be one of this class's menu id constants. If null,
* the default menus are created.
*/
public void createMenus(JMenuBar menuBar,
MTextPanel textPanel,
Frame frame,
int[] menus) {
if (menus == null) {
menus = defaultMenus;
}
synchronized (MItem.LOCK) {
fMenuBar = menuBar;
doCreateMenus(textPanel, frame, menus);
fMenuBar = null;
}
}
protected void handleAddMenu(String key) {
JMenu menu = new JMenu(ResourceUtils.getResourceString(key));
fMenuBar.add(menu);
MItem.setItemFactory(new SwingMenuFactory(menu));
}
protected DialogFactory createObjectDialogFactory(final String dialogTitle,
final String dialogMessage,
final Object key,
final boolean character,
final String[] names,
final Object[] values) {
final Frame dialogParent = fDialogParent;
return new DialogFactory() {
public Window createDialog(MTextPanel textPanel) {
return new JObjectDialog(dialogParent,
dialogTitle,
dialogMessage,
textPanel,
key,
character,
names,
values);
}
};
}
protected DialogFactory createNumberDialogFactory(final String dialogTitle,
final String dialogMessage,
final Object key,
final boolean character) {
final Frame dialogParent = fDialogParent;
final MTextPanel textPanel = fTextPanel;
return new DialogFactory() {
public Window createDialog(MTextPanel fTextPanel) {
return new JNumberDialog(dialogParent,
dialogTitle,
dialogMessage,
fTextPanel,
key,
character,
1);
}
};
}
protected DialogFactory createAboutDialogFactory() {
return new DialogFactory() {
public Window createDialog(MTextPanel textPanel) {
String title = ResourceUtils.getResourceString(FrameResources.ABOUT_TITLE);
return new JMessageDialog(title, AboutText.getAboutText());
}
};
}
}

View file

@ -0,0 +1,108 @@
/*
* @(#)$RCSfile: SwingMenuFactory.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:46:22 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.swingui;
import java.awt.Event;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.ibm.richtext.uiimpl.resources.MenuData;
import com.ibm.richtext.uiimpl.EventListener;
import com.ibm.richtext.uiimpl.MItem;
import com.ibm.richtext.uiimpl.MItem.ItemFactory;
final class SwingMenuFactory implements ItemFactory {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private final class SwingMItem extends MItem {
private JMenuItem fItem;
SwingMItem(JMenuItem item) {
fItem = item;
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleSelected();
}
});
}
protected void handleSelected() {
super.handleSelected();
}
public final void setEnabled(boolean enabled) {
fItem.setEnabled(enabled);
}
public void setState(boolean checked) {
try {
JCheckBoxMenuItem item = (JCheckBoxMenuItem) fItem;
item.setState(checked);
}
catch(ClassCastException e) {
throw new Error("Cannot perform setChecked on non-checkbox item");
}
}
}
private JMenu fMenu;
SwingMenuFactory(JMenu menu) {
fMenu = menu;
}
private MItem handleCreate(JMenuItem item,
MenuData menuData) {
if (menuData.hasShortcut()) {
KeyStroke ks = KeyStroke.getKeyStroke(menuData.getShortcutKeyCode(),
Event.CTRL_MASK);
item.setAccelerator(ks);
}
fMenu.add(item);
return new SwingMItem(item);
}
public MItem createItem(MenuData menuData) {
return handleCreate(new JMenuItem(menuData.getName()), menuData);
}
public MItem createCheckboxItem(MenuData menuData) {
return handleCreate(new JCheckBoxMenuItem(menuData.getName()), menuData);
}
public void createSeparator() {
fMenu.add(new JSeparator());
}
}

View file

@ -0,0 +1,5 @@
<html>
<body bgcolor="white">
Provides classes for building a Swing-based user interface for a TextPanel.
</body>
</html>