Friday, April 4, 2008

Intralink Scripting: Font Size Does Matter!

Using Java Swing Utilities to make the Intralink GUI text more readable

The font in the Intralink client is a little small for most people, and as a result changing the font size is a question that pops up from time to time. Unfortunately, Intralink doesn't provide a way to do this directly, which makes most people just give up.

Since we're dealing with Java, almost anything is possible, and changing the font is one of those things. There's basically three main steps: 1) Setup the font object to use, 2) Tell the UIManager is use the new font details, 3) Force a full GUI redraw. It's easiest to do this when Intralink is starting, then the user is good to go from that point on.

Steps 1 & 2 are encompassed into one function setMenuFontSizeDefault(). The first task is to obtain the font object used for menus ("Menu.font"). Then the details (Name, Style, Size) are extracted. The function is changing only the size, but a new font could be used as well (check the comments). With the changed font details, a new FontUIResource object is created.

The next task is to associate the new font object with many different font attributes, obviously in what looks to be a brute force attack.

private void setMenuFontSizeDefault(int size) {

// get font style and name from current menu font
//
String BaseFontSource = "Menu.font";
Font fontCurrent = UIManager.getFont(BaseFontSource);
String name = fontCurrent.getName();
int style = fontCurrent.getStyle();

String styleName = "(?)";
if (style == Font.PLAIN) { styleName = "(Plain)"; }
if (style == Font.ITALIC) { styleName = "(Italic)"; }
if (style == Font.BOLD) { styleName = "(Bold)"; }

System.out.println( "Current " + BaseFontSource + " name/style/size: "
+ name + "/" + style + styleName + "/" + fontCurrent.getSize() );

// Override font name and/or style by uncommenting one of these
// name = "Times New Roman";
// style = Font.BOLD; styleName = "(Bold)";

System.out.println( "Changing name/style/size to: " + name + "/" + style + styleName + "/" + size );

// create similar font with the specified size
//
FontUIResource fontResourceNew = new FontUIResource(name, style, size);


// change UI defaults for all types of menu components
//
UIManager.put("Button.font", fontResourceNew);
UIManager.put("CheckBox.font", fontResourceNew);
UIManager.put("CheckBoxMenuItem.acceleratorFont", fontResourceNew);
UIManager.put("CheckBoxMenuItem.font", fontResourceNew);
UIManager.put("ComboBox.font", fontResourceNew);
UIManager.put("DesktopIcon.font", fontResourceNew);
UIManager.put("EditorPane.font", fontResourceNew);
UIManager.put("FormattedTextField.font", fontResourceNew);
UIManager.put("InternalFrame.titleFont", fontResourceNew);
UIManager.put("Label.font", fontResourceNew);
UIManager.put("List.font", fontResourceNew);
UIManager.put("Menu.acceleratorFont", fontResourceNew);
UIManager.put("Menu.font", fontResourceNew);
UIManager.put("MenuBar.font", fontResourceNew);
UIManager.put("MenuItem.acceleratorFont", fontResourceNew);
UIManager.put("MenuItem.font", fontResourceNew);
UIManager.put("OptionPane.buttonFont", fontResourceNew);
UIManager.put("OptionPane.messageFont", fontResourceNew);
UIManager.put("PasswordField.font", fontResourceNew);
UIManager.put("PopupMenu.font", fontResourceNew);
UIManager.put("ProgressBar.font", fontResourceNew);
UIManager.put("RadioButton.font", fontResourceNew);
UIManager.put("RadioButtonMenuItem.acceleratorFont", fontResourceNew);
UIManager.put("RadioButtonMenuItem.font", fontResourceNew);
UIManager.put("Spinner.font", fontResourceNew);
UIManager.put("TabbedPane.font", fontResourceNew);
UIManager.put("Table.font", fontResourceNew);
UIManager.put("TableHeader.font", fontResourceNew);
UIManager.put("TextArea.font", fontResourceNew);
UIManager.put("TextField.font", fontResourceNew);
UIManager.put("TextPane.font", fontResourceNew);
UIManager.put("TitledBorder.font", fontResourceNew);
UIManager.put("ToggleButton.font", fontResourceNew);
UIManager.put("ToolBar.font", fontResourceNew);
UIManager.put("ToolTip.font", fontResourceNew);
UIManager.put("Tree.font", fontResourceNew);
UIManager.put("Viewport.font", fontResourceNew);
UIManager.put("JTitledPanel.title.font", fontResourceNew);

}

Probably a better way, but it works!


Step 3 is accomplished with the updateAllFrames() function. With the frames gathered from Frame.getFrames(), the font change is forced upon the frame (and the frames windows, if any) using SwingUtilities.updateComponentTreeUI().

private void updateAllFrames () {

Frame frames[] = Frame.getFrames();

for (int i = 0; i < frames.length; i++) {

if ( frames[i].getTitle().equals("") frames[i].getTitle().equals("Pending RTP Forms") ) {
continue;
}

// Update the frames
try {
SwingUtilities.updateComponentTreeUI(frames[i]);
}
catch (Exception e) {
// Exception thrown, skip it
}

// Update the frame's windows
Window windows[] = frames[i].getOwnedWindows();
for (int j = 0; j < windows.length; j++) {
try {
SwingUtilities.updateComponentTreeUI(windows[j]);
}
catch (Exception e) {
// Exception thrown, skip it
}
}

}

}

The two function are called in this order, as an example:

setMenuFontSizeDefault(15); // set font size to 15
updateAllFrames();



These import statement might be necessary as well:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;





No comments: