/*
* Licensed Materials - Property of Rogue Wave Software, Inc.
* © Copyright Rogue Wave Software, Inc. 2014, 2017
* © Copyright IBM Corp. 2009, 2014
* © Copyright ILOG 1996, 2009
* All Rights Reserved.
*
* Note to U.S. Government Users Restricted Rights:
* The Software and Documentation were developed at private expense and
* are "Commercial Items" as that term is defined at 48 CFR 2.101,
* consisting of "Commercial Computer Software" and
* "Commercial Computer Software Documentation", as such terms are
* used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
* as applicable.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.TextAttribute;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import ilog.views.IlvApplyObject;
import ilog.views.IlvGraphic;
import ilog.views.IlvGraphicEnumeration;
import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.IlvPoint;
import ilog.views.event.ManagerSelectionChangedEvent;
import ilog.views.event.ManagerSelectionListener;
import ilog.views.graphic.IlvLabel;
import ilog.views.graphic.IlvText;
import ilog.views.graphic.IlvTextSelection;
import ilog.views.interactor.IlvSelectInteractor;
/**
* This sample shows how to use the inplace text editor.
*/
public class TextEditor extends JFrame
{
IlvManager manager;
IlvManagerView mgrview;
JComboBox<String> fontSelector;
JComboBox<String> colorSelector;
boolean blockSelectors = false;
public TextEditor()
{
// Creates the manager.
manager = new IlvManager();
Font font = new Font("SansSerif", Font.BOLD, 14);
IlvLabel label = new IlvLabel(new IlvPoint(200, 40), "Welcome to inplace text editor test panel!");
label.setFont(font);
label.setForeground(Color.blue);
manager.addObject(label, false);
font = new Font("Arial", Font.ITALIC, 24);
label = new IlvLabel(new IlvPoint(200, 80), "I am an IlvLabel. Click on me to edit!");
label.setFont(font);
manager.addObject(label, false);
font = new Font("SansSerif", Font.CENTER_BASELINE, 15);
label = new IlvLabel(new IlvPoint(200, 120), "Sorry, you can not edit me but you can move me;-))");
label.setForeground(Color.green);
label.setFont(font);
manager.addObject(label, false);
manager.setEditable(label, false);
font = new Font("SansSerif", Font.CENTER_BASELINE, 18);
IlvText text = new IlvText(new IlvPoint(20, 160), "Hi, I am an IlvText. I have several lines.\nYou can click on me and edit me ;-))\nEnjoy...");
text.setWrappingWidth(200);
text.setWrappingMode(IlvText.WRAP_WORD);
text.setForeground(Color.red);
text.setFont(font);
text.addLabelAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON,
51, 56);
text.addLabelAttribute(TextAttribute.FONT,
new Font("SansSerif", Font.ITALIC | Font.CENTER_BASELINE, 18),
12, 19);
text.addLabelAttribute(TextAttribute.FONT,
new Font("Serif", Font.ITALIC | Font.CENTER_BASELINE, 18),
28, 35);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.blue,
28, 41);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.black,
43, 46);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.green.darker(),
57, 62);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.yellow.darker(),
63, 67);
text.addLabelAttribute(TextAttribute.BACKGROUND,
Color.cyan,
67, 71);
// text.addLabelAttribute(TextAttribute.STRIKETHROUGH,
// TextAttribute.STRIKETHROUGH_ON,
// 67, 71);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.cyan.darker(),
80, 81);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.yellow.darker(),
81, 82);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.green.darker(),
82, 83);
text.addLabelAttribute(TextAttribute.FOREGROUND,
Color.blue,
83, 84);
// text.setUnderline(true);
// text.setStrikethrough(true);
manager.addObject(text, false);
font = new Font("SansSerif", Font.BOLD, 24);
text = new IlvText(new IlvPoint(220, 160), "I AM AN OUTLINED\nTEXT!");
text.setFont(font);
text.setForeground(Color.orange);
text.setOutlineColor(Color.blue);
text.setOutlineThickness(0);
text.setOutlineMode(IlvText.OUTLINE_FRONT);
manager.addObject(text, false);
font = new Font("SansSerif", Font.BOLD, 24);
text = new IlvText(new IlvPoint(220, 220), "I AM AN OUTLINE\nONLY TEXT!");
text.setFont(font);
text.setOutlineColor(Color.RED);
text.setOutlineThickness(1);
text.setOutlineMode(IlvText.OUTLINE_ONLY);
manager.addObject(text, false);
// Creates a view associated to the manager
mgrview = new IlvManagerView(manager);
mgrview.setBackground(Color.white);
// Install View Interactor
IlvSelectInteractor inter = new IlvSelectInteractor();
inter.setOpaqueMove(true);
inter.setOpaqueResize(true);
mgrview.setInteractor(inter);
// Add the manager view:
getContentPane().add(mgrview, BorderLayout.CENTER);
getContentPane().add(createTopControls(), BorderLayout.NORTH);
// Add a listener to the manager that sets the font and color selector back
manager.addManagerSelectionListener(new ManagerSelectionListener() {
Override
public void selectionChanged(ManagerSelectionChangedEvent event)
{
blockSelectors = true;
boolean hasIlvTextSelected = false;
IlvManager manager = event.getManager();
IlvGraphicEnumeration e = manager.getSelectedObjects();
while (e.hasMoreElements()) {
IlvGraphic g = e.nextElement();
if (g instanceof IlvText) {
hasIlvTextSelected = true;
break;
}
}
if (fontSelector != null)
fontSelector.setEnabled(hasIlvTextSelected);
if (colorSelector != null)
colorSelector.setEnabled(hasIlvTextSelected);
if (fontSelector != null)
fontSelector.setSelectedIndex(-1);
if (colorSelector != null)
colorSelector.setSelectedIndex(-1);
blockSelectors = false;
}
});
}
/**
* Creates the controls at the top level of the window.
*/
private JPanel createTopControls()
{
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
String[] fitems = new String[] { "SanSerif14", "SanSerif18",
"Serif14", "Serif18" };
fontSelector = new JComboBox<String>(fitems);
panel.add(new JLabel("Font:"));
panel.add(fontSelector);
fontSelector.addActionListener(new ActionListener() {
Override
public void actionPerformed(ActionEvent event) {
if (blockSelectors) return;
SuppressWarnings("unchecked")
JComboBox<String> comboBox = (JComboBox<String>)event.getSource();
String fontName = (String)comboBox.getSelectedItem();
Font font = null;
if (fontName.equals("SanSerif14"))
font = new Font("SansSerif", Font.CENTER_BASELINE, 14);
else if (fontName.equals("SanSerif18"))
font = new Font("SansSerif", Font.CENTER_BASELINE, 18);
else if (fontName.equals("Serif14"))
font = new Font("SansSerif", Font.CENTER_BASELINE, 14);
else if (fontName.equals("Serif18"))
font = new Font("SansSerif", Font.CENTER_BASELINE, 18);
if (font != null) {
IlvGraphicEnumeration e = manager.getSelectedObjects();
while (e.hasMoreElements()) {
IlvGraphic g = e.nextElement();
if (g instanceof IlvText) {
final IlvText text = (IlvText)g;
IlvTextSelection selection =
(IlvTextSelection)manager.getSelection(text);
final IlvTextSelection.Range r = selection.getRange();
if (!r.isEmpty() && !r.isCollapsed()) {
text.getGraphicBag().applyToObject(text, new IlvApplyObject() {
Override
public void apply(IlvGraphic g, Object arg) {
text.addLabelAttribute(TextAttribute.FONT,
arg,
r.from, r.to);
}
}, font, true);
}
}
}
}
}
});
String[] citems = new String[] { "Red", "Blue", "Green" };
colorSelector = new JComboBox<String>(citems);
panel.add(new JLabel("Color:"));
panel.add(colorSelector);
colorSelector.addActionListener(new ActionListener() {
Override
public void actionPerformed(ActionEvent event) {
if (blockSelectors) return;
SuppressWarnings("unchecked")
JComboBox<String> comboBox = (JComboBox<String>)event.getSource();
String colorName = (String)comboBox.getSelectedItem();
Color color = null;
if (colorName.equals("Red"))
color = Color.red;
else if (colorName.equals("Blue"))
color = Color.blue;
else if (colorName.equals("Green"))
color = Color.green.darker();
if (color != null) {
IlvGraphicEnumeration e = manager.getSelectedObjects();
while (e.hasMoreElements()) {
IlvGraphic g = e.nextElement();
if (g instanceof IlvText) {
final IlvText text = (IlvText)g;
IlvTextSelection selection =
(IlvTextSelection)manager.getSelection(text);
final IlvTextSelection.Range r = selection.getRange();
if (!r.isEmpty() && !r.isCollapsed()) {
text.getGraphicBag().applyToObject(text, new IlvApplyObject() {
Override
public void apply(IlvGraphic g, Object arg) {
text.addLabelAttribute(TextAttribute.FOREGROUND,
arg,
r.from, r.to);
}
}, color, true);
}
}
}
}
}
});
return panel;
}
public static void main(String[] args)
{
// This sample uses JViews Diagrammer features. When deploying an
// application that includes this code, you need to be in possession
// of a Rogue Wave JViews Diagrammer Deployment license.
// IlvProductUtil.DeploymentLicenseRequired(
// IlvProductUtil.JViews_Diagrammer_Deployment);
// Sun recommends that to put the entire GUI initialization into the
// AWT thread
SwingUtilities.invokeLater(
new Runnable() {
Override
public void run() {
JFrame frame = new TextEditor();
frame.setTitle("JViews Inplace Text Editor Sample");
frame.setSize(450,350);
frame.setVisible(true);
frame.addWindowListener(new ExitOnWindowClosing());
}
});
}
static class ExitOnWindowClosing extends WindowAdapter
{
public ExitOnWindowClosing()
{}
Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}