Menu-controlled visibility of a prototype having a multirep behavior

Article ID: 2234
Last updated: 31 Oct, 2023
Article ID: 2234
Last updated: 31 Oct, 2023
Revision: 5
Views: 1044
Posted: 23 Jun, 2010
by Dean J.
Updated: 31 Oct, 2023
by Gifford C.

Question

Menu-controlled visibility of a prototype having a multirep behavior.

Answer

  • A prototype is loaded from the file multi.ivl and added to an IlvGroupBag.
  • Also, a menu is displayed:
    • The first menu item gives the choice to hide/show the prototype (by setting the "visible" state).
    • In the set of menu items below, the first controls the state of the multirep behavior (by setting the "multi" state)
  • To run the program, put the file multi.ivl in the same folder as the file Main.java

import ilog.views.*;
import ilog.views.io.*;
import ilog.views.prototypes.*;
import ilog.views.swing.*;
import ilog.views.util.IlvProductUtil;

import java.awt.event.*;
import javax.swing.*;
public class Main
{
// Comment the following code out if using JViews 8.6 or earlier.
static {
IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.IBM_ILOG_JViews_Enterprise_Deployment);
}

public static final String VISIBLE = "visible";
public static final String MY_VISIBLE = "my_visible";
public static final String MULTIREP = "multiRep";
public static void main(String args[]) throws IlvValueException, 
IlvReadFileException
{
JFrame window = new JFrame("Main");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(20, 20, 400, 300);
// Add the manager to the window
IlvManager manager = new IlvManager();
IlvManagerView managerView = new IlvManagerView(manager);
IlvJScrollManagerView managerScrView = new IlvJScrollManagerView(managerView);
window.getContentPane().add(managerScrView);
// Get the prototype, add a value behavior, and initialize its values
IlvPrototype prototype = IlvPrototypeLibrary.getPrototype("multi", "multi");
prototype.addBehavior(new IlvValueBehavior(MY_VISIBLE, Boolean.class));
prototype.set(MY_VISIBLE, true);
prototype.set(MULTIREP, "3");
prototype.move(150, 50);
// Add a menu bar with a menu that can change the state
addMenu(window, new MyMenuListener(prototype));
// Add the prototype to the group bag of the manager
new IlvGroupBag(manager).addGroup(prototype);
window.setVisible(true);
}
private static void addMenu(JFrame window, MyMenuListener listener)
{
JMenu menu = new JMenu("Change state");
window.setJMenuBar(new JMenuBar());
window.getJMenuBar().add(menu);
JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem(VISIBLE);
checkBox.addItemListener(listener);
menu.add(checkBox);
checkBox.setSelected(true);
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem[] radios = new JRadioButtonMenuItem[4];
for(int i = 0; i < 4; i++) {
radios[i] = new JRadioButtonMenuItem("State " + i);
radios[i].setActionCommand("" + i);
radios[i].addActionListener(listener);
group.add(radios[i]);
menu.add(radios[i]);
}
radios[3].setSelected(true);
}
}
class MyMenuListener implements ItemListener, ActionListener
{
private IlvPrototype _prototype;
MyMenuListener(IlvPrototype prototype)
{
_prototype = prototype;
}
public void itemStateChanged(ItemEvent event)
{
try {
// Change the visible state of the prototype, keeping its multirep state
if (event.getStateChange() == ItemEvent.SELECTED) {
int multirep = _prototype.getInt(Main.MULTIREP);
_prototype.set(Main.VISIBLE, true);
_prototype.set(Main.MY_VISIBLE, true);
_prototype.set(Main.MULTIREP, multirep);
} else {
_prototype.set(Main.VISIBLE, false);
_prototype.set(Main.MY_VISIBLE, false);
}
} catch(IlvValueException e) {}
}
public void actionPerformed(ActionEvent event)
{
try {
// Change the multirep state of the prototype, keeping its visible state
_prototype.set(Main.MULTIREP, event.getActionCommand());
if(! _prototype.getBoolean(Main.MY_VISIBLE)) {
_prototype.set(Main.VISIBLE, false);
}
} catch(IlvValueException e) {}
}
}
This article was:   Helpful | Not helpful
Report an issue
Article ID: 2234
Last updated: 31 Oct, 2023
Revision: 5
Views: 1044
Posted: 23 Jun, 2010 by Dean J.
Updated: 31 Oct, 2023 by Gifford C.
Also listed in


Others in this category