Files
GTSchoolShit/CS2335/lab4/src/netpaint/client/gui/FontSelectionPanel.java
2025-06-07 01:59:34 -04:00

428 lines
12 KiB
Java

package netpaint.client.gui;
import java.util.Vector;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
import javax.swing.JToggleButton;
/**
* Class FontSelectionPanel: This panel contains all of the controls
* necessary to allow the user to specify a font - the sizes, names,
* and styles. It also has a Font data field which can be accessed
* through the appropriate accessor and will always contain the
* currently selected font.
*
* <PRE>
* Revision History:
* v1.0 (Feb. 08, 2004) - Created the FontSelectionPanel class
* </PRE>
*
* @author <A HREF="mailto:gtg308i@mail.gatech.edu">Vladimir Urazov</A>
* @version Version 1.0, Feb. 08, 2004
*/
public final class FontSelectionPanel extends JPanel
implements ActionListener {
/**
* This is the currently selected font.
*/
private Font currentFont;
/**
* Contains the name of the currently selected font.
*/
private String currentFontName;
/**
* The currently selected font size.
*/
private float currentFontSize;
/**
* Contains true if the bold toggle is currently turned on.
*/
private boolean boldSelected;
/**
* Contains true if the italic toggle button is currently turned on.
*/
private boolean italicSelected;
/**
* This is the button that will be clicked when the user wants to
* finalize the text input.
*/
private JButton finalizeButton;
/**
* Creates a new <code>FontSelectionPanel</code> instance.
*/
public FontSelectionPanel() {
super();
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(makeFontNameBox());
this.add(makeFontSizeBox());
this.add(makeStyleSwitchPanel());
this.add(makeFinalizePanel());
updateCurrentFont();
}
/**
* Makes a combo box that allows you to select the font type.
*
* @return a <code>JComboBox</code> value
*/
private JComboBox makeFontNameBox() {
JComboBox result = null;
String[] allFamilies = GraphicsEnvironment
.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
result = new JComboBox(allFamilies);
result.setActionCommand("font_name_select");
result.addActionListener(this);
currentFontName = allFamilies[0];
return result;
}
/**
* Returns a combo box filled with some sizes for the fonts.
*
* @return a <code>JComboBox</code> value
*/
private JComboBox makeFontSizeBox() {
JComboBox result = null;
Vector sizes = new Vector();
sizes.add(new Integer(8));
sizes.add(new Integer(9));
sizes.add(new Integer(10));
sizes.add(new Integer(11));
sizes.add(new Integer(12));
sizes.add(new Integer(14));
sizes.add(new Integer(16));
sizes.add(new Integer(18));
sizes.add(new Integer(20));
sizes.add(new Integer(22));
sizes.add(new Integer(24));
sizes.add(new Integer(26));
sizes.add(new Integer(28));
sizes.add(new Integer(36));
sizes.add(new Integer(48));
sizes.add(new Integer(72));
result = new JComboBox(sizes);
result.setEditable(true);
result.setSelectedIndex(4);
result.setActionCommand("font_size_select");
result.addActionListener(this);
currentFontSize = 12;
return result;
}
/**
* Creates a new style button with the specified name, tool tip, and icon.
*
* @param name a <code>String</code> value
* @param toolTip a <code>String</code> value
* @param image a <code>String</code> value
* @return a <code>JToggleButton</code> value
*/
private JToggleButton makeStyleButton(final String name,
final String toolTip,
final String image) {
JToggleButton result = new JToggleButton();
result.setActionCommand(name);
result.addActionListener(this);
//Set up the image icon:
BufferedImage bim = ImageLoader.getInstance().loadSystemImage(image);
if (bim != null) {
//Successfully loaded the image
result.setIcon(new ImageIcon(bim));
} else {
//Could not load image...
result.setText(name);
result.setFont(result.getFont().deriveFont(Font.ITALIC));
result.setHorizontalAlignment(JButton.HORIZONTAL);
}
result.setToolTipText(name + ": " + toolTip);
result.setFocusPainted(false);
return result;
}
/**
* Makes the panel that contains toggle buttons to allow the user to
* select different font styles.
*
* @return a <code>JPanel</code> value
*/
private JPanel makeStyleSwitchPanel() {
JPanel result = new JPanel();
JToggleButton currentButton = null;
result.setLayout(new BoxLayout(result, BoxLayout.X_AXIS));
//Bold
currentButton = makeStyleButton(ClientUISettings.FONT_BOLD_NAME,
ClientUISettings.FONT_BOLD_TTIP,
ClientUISettings.FONT_BOLD_ICON);
result.add(currentButton);
boldSelected = false;
//Italic
currentButton = makeStyleButton(ClientUISettings.FONT_ITALIC_NAME,
ClientUISettings.FONT_ITALIC_TTIP,
ClientUISettings.FONT_ITALIC_ICON);
result.add(currentButton);
italicSelected = false;
/*
//Underlined
currentButton = makeStyleButton(ClientUISettings.FONT_UNDERLINED_NAME,
ClientUISettings.FONT_UNDERLINED_TTIP,
ClientUISettings.FONT_UNDERLINED_ICON);
result.add(currentButton);
*/
return result;
}
/**
* Makes a panel that contains the button to finalize the input of text.
*
* @return a <code>JPanel</code> value
*/
private JPanel makeFinalizePanel() {
JPanel result = new JPanel();
finalizeButton = new JButton();
finalizeButton.setActionCommand(ClientUISettings.FONT_FINALIZE_NAME);
finalizeButton.addActionListener(this);
//Set up the image icon:
BufferedImage bim = ImageLoader.getInstance()
.loadSystemImage(ClientUISettings.FONT_FINALIZE_ICON);
if (bim != null) {
//Successfully loaded the image
finalizeButton.setIcon(new ImageIcon(bim));
} else {
//Could not load image...
finalizeButton.setText(ClientUISettings.FONT_FINALIZE_NAME);
finalizeButton.setFont(finalizeButton.getFont().deriveFont(Font.ITALIC));
finalizeButton.setHorizontalAlignment(JButton.HORIZONTAL);
}
finalizeButton.setToolTipText(ClientUISettings.FONT_FINALIZE_NAME + ": "
+ ClientUISettings.FONT_FINALIZE_TTIP);
finalizeButton.setFocusPainted(false);
finalizeButton.setEnabled(false);
result.add(finalizeButton);
return result;
}
/**
* Returns the font currently slected in the combo box.
*
* @return a <code>Font</code> value
*/
public Font getSelectedFont() {
Font[] allFonts = null;
allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
//Find the font we want:
for (int i = 0; i < allFonts.length; i++) {
if (allFonts[i].getFamily().equals(currentFontName)) {
return allFonts[i];
}
}
//No font found...
return null;
}
/**
* Reads from the inputs and derives the new Font from the selected
* values.
*/
private synchronized void updateCurrentFont() {
Font baseFont = getSelectedFont();
Font oldFont = currentFont;
int style = 0;
boolean rerender = true;
if (baseFont == null) {
System.out.println("FontSelectionPanel: Could not get base font... "
+ "Old font preserved.");
return;
}
//Make sure we don't go into an infinite loop if this is the first
//time updating the font.
if (currentFont == null) {
rerender = false;
}
if (boldSelected) {
style |= Font.BOLD;
}
if (italicSelected) {
style |= Font.ITALIC;
}
currentFont = baseFont.deriveFont(style, currentFontSize);
if (currentFont == null) {
System.out.println("FontSelectionPanel: Could not update font...");
currentFont = oldFont;
}
if (ClientUISettings.VERBOSE) {
System.out.println("FontSelectionPanel: Current font updated:");
System.out.println("\t" + currentFont);
}
if (rerender) {
//Tell the graphics panel to update.
ClientUI.getInstance().getGraphicsPanel().markForRerender(false);
}
}
/**
* Returns the currently selected font.
*
* @return a <code>Font</code> value
*/
public Font getCurrentFont() {
return currentFont;
}
/**
* Enables the finalize button.
*/
public void enableFinalize() {
finalizeButton.setEnabled(true);
}
/**
* Disnables the finalize button.
*/
public void disableFinalize() {
finalizeButton.setEnabled(false);
}
/**
* Invoked when an action occurs.
*
* @param e the event associated with the action.
*/
public void actionPerformed(final ActionEvent e) {
String action = e.getActionCommand();
//The finalize button check will go here and will return
//before the next batch of if statements if it evaluates.
if (action.equals(ClientUISettings.FONT_FINALIZE_NAME)) {
disableFinalize();
ClientUI.getInstance().getToolPanel().getCurrentTool().finish();
return;
}
if (action.equals("font_name_select")) {
//Chose a different font name...
JComboBox src = (JComboBox) e.getSource();
currentFontName = (String) src.getSelectedItem();
if (ClientUISettings.VERBOSE) {
System.out.println("FontSelectionPanel: New font name - "
+ currentFontName);
}
} else if (action.equals("comboBoxEdited")) {
//Chose a different font size...
JComboBox src = (JComboBox) e.getSource();
String text = (String) src.getSelectedItem();
try {
currentFontSize = Float.parseFloat(text);
} catch (NumberFormatException nfe) {
System.out.println("FontSelectionPanel: Got bad size value - " + text);
src.setSelectedItem(new Integer((int) currentFontSize));
//Tell the client to only type in acceptable numbers.
ClientUI.getInstance()
.showWarning("Bad size input",
"Only decimal numbers are allowed as input into the fon"
+ "t size pane. Please input a valid decimal number.");
return;
}
} else if (action.equals("font_size_select")) {
//Chose a different font size...
JComboBox src = (JComboBox) e.getSource();
try {
currentFontSize = ((Integer) src.getSelectedItem()).intValue();
} catch (Exception exc) {
if (ClientUISettings.VERBOSE) {
System.out.println("FontSelectionPanel: Got an exception:");
System.out.println("\t" + exc.getMessage());
}
return;
}
if (ClientUISettings.VERBOSE) {
System.out.println("FontSelectionPanel: New font size - "
+ currentFontSize);
}
} else if (action.equals(ClientUISettings.FONT_BOLD_NAME)) {
boldSelected = !boldSelected;
if (ClientUISettings.VERBOSE) {
System.out.println("FontSelectionPanel: Bold selected - "
+ boldSelected);
}
} else if (action.equals(ClientUISettings.FONT_ITALIC_NAME)) {
italicSelected = !italicSelected;
if (ClientUISettings.VERBOSE) {
System.out.println("FontSelectionPanel: Italic selected - "
+ italicSelected);
}
} else {
System.out.println("FontSelectionPanel: Unknown action command - "
+ action);
}
updateCurrentFont();
}
}