I have problem in understanding a composite.
In Frame class i have verticalPanel(vp) when vp is loaded , getAction and get Button is visible on vp
WHen click on button there is a getTree is executed and there is treeC class initialized where there is customised tree. and treeitem.
I want to use action object in class TreeC
How to do it.
Plz Help.
public class Frame{
public frame () {
initWidget(getFramePanel());
}
Private VerticalalPanel getFramePanel() {
if (vp== null) {
vp= new VerticalalPanel();
vp.setSize("1442px", "750px");
vp.add(getAction());// **are composites**
vp.add(getButton) // **are composite**
}
return vp;
private Action getAction() {
if (action == null) {
action = new Action(); // In action class there are 7 buttons and 2 methods //setDisplayRepository(), and setDisplayFolder()
action.setDisplayRepository();
}
return action;
}
}
private Button getButton() {
if (btn == null) {
btn = new Button("Click");
btnProperties.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
hp.add(getTree());
}
});
btn.setSize("37px", "36px");
}
return btnProperties;
}
private TreeCmis getTreeC() {
if (treeC == null) {
treeC = new TreeC();
treeC.setWidth("360px");
}
return treeCmis;
}
}
public class TreeC extends Composite{
private Tree repo;
//constructor
public TreeC {
createTree()
}
Void createTree(){
/* here i need to to use the object action declared in frame class
For using action.setDisplayfolder*/
}
}
The simplest way is:
public class TreeC extends Composite{
private Tree repo;
private Action action;
//constructor
public TreeC(Action action) {
this.action = action;
createTree()
}
void createTree(){
/* here i need to to use the object action declared in frame class
For using action.setDisplayfolder*/
}
}
When Create instance treeC = new TreeC(action);
Related
I've created a simple terminal app using lanterna that simply displays a custom button. But even though in the custom button's class I extend the Button class and override the Button class' createDefaultRenderer method to return an instance of the Button class' FlatButtonRenderer class, it is displaying the button using the DefaultButtonRenderer class.
Can you help me understand how to create a custom button in lanterna that displays the button using the FlatButtonRenderer?
import java.io.IOException;
import java.util.ArrayList;
import com.googlecode.lanterna.terminal.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.gui2.*;
class Test {
public static void main(String[] args) {
DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory();
Screen screen = null;
try {
screen = terminalFactory.createScreen();
screen.startScreen();
final WindowBasedTextGUI textGUI = new MultiWindowTextGUI(screen);
final Window window = new GUIAppWindow();
textGUI.addWindowAndWait(window);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if(screen != null) {
try {
screen.stopScreen();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
private static class GUIAppWindow extends BasicWindow {
GUIAppWindow() {
ArrayList<Window.Hint> hints = new ArrayList<>();
hints.add(Window.Hint.CENTERED);
setHints(hints);
Panel mainPanel = new Panel(new LinearLayout(Direction.VERTICAL));
XButton b = new XButton(new String("."));
b.addListener(new ButtonHandler("data"));
mainPanel.addComponent(b);
this.setComponent(mainPanel);
}
private class XButton extends Button {
public XButton(String label) {
super(label);
}
#Override
protected ButtonRenderer createDefaultRenderer() {
return new Button.FlatButtonRenderer();
}
}
private class ButtonHandler implements Button.Listener {
final String loc;
ButtonHandler(String loc) {
this.loc = loc;
}
public void onTriggered(Button button) {
button.setLabel(button.getLabel().equals(".") ? "x" : new String("."));
}
}
}
}
I believe the correct way to use FlatButtonRenderer is to call setRenderer().
private class XButton extends Button {
public XButton(String label) {
super(label);
setRenderer(new Button.FlatButtonRenderer());
}
}
Here is a class View of the MVC paradigm, the class consist of 2 JDialogs, to be opened on click of JMenuItem - addEvent and editEvent.
public class EventView extends javax.swing.JFrame {
private javax.swing.JDialog addDialog;
private javax.swing.JDialog editDialog;
private EventModel model;
/** Constructor */
public EventView(EventModel model) {
initComponents();
this.model = model;
updateEventTable();
}
public void addEventListener(ActionListener al) {
addEventButton.addActionListener(al);
}
/* public void clearListener(ActionListener cl) {
clearEventButton.addActionListener(cl);
}*/
public void addDialog(ActionListener ae) {
addEvent.addActionListener(ae);
}
public void editDialog(ActionListener ee) {
editEvent.addActionListener(ee);
}
}
The controller class handles the user interaction with listeners.
public class EventController implements ActionListener {
//... The Controller needs to interact with both the Model and View.
private EventModel model;
private EventView view;
/** Constructor */
public EventController(EventModel model, EventView v){
model = new EventModel();
view = v;
//... Add listeners to the view.
view.addEventListener(new addEventListener());
//view.clearListener(new clearEventListener());
view.addDialog(new addDialogListener());
view.editDialog(new editDialogListener());
}
class addEventListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String name = "";
String date;
String start="";
String end="";
String venue="";
String details="";
String opportunities="";
String moreOppor="";
try {
name = view.getEventName();
date = view.eventDate().toString();
start = view.startTime();
end = view.endTime();
venue = view.locationWhere();
details = view.getDetails();
opportunities = view.getOpportunities();
moreOppor = view.getMore();
model.addEvent(name,date,start,venue,details,opportunities,moreOppor,end);
view.showSuccess("Event Added!");
} catch (Exception ex) {
view.showError(ex);
}
}
}
class addDialogListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("");
}
}
class editDialogListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("");
}
}
I have two question in relation to this module:
EventController is showing an error that it is not abstract and does not override abstract method actionPerformed when I believe I did. Correct me if im wrong. I do have an additional JMenuItem called deleteEvent but I havent touched that yet. Working on NetbeansIDE F.Y.I
I would like to replace the lines System.out.println(""); with something that will allow me to display the dialog addDialog of the view class but I cannot access the component. how to do this? I've tried view. but it doesnt show up allow for setVisible(true) .
Your compiler is quite right: EventController does not declare a public void actionPerformed(ActionEvent e) method. It does have two inner classes that have the method, though, but that doesn't count.
The way you have named your class, addEventListener, suggests that you really meant to call the method addEventListener instead of declare a class, but no definite suggestion can be given based on your code.
1) You forgot to implement the actionPerformed() method in the EventController Class.
2) You should set the view as an argument of addDialogListener and editDialogListener.
Something like :
public class addDialogListener implements ActionListener {
private EventView view;
public addDialogListener(EventView view){
this.view = view;
}
#Override
public void actionPerformed(ActionEvent e) {
view.doWhatever();
}
}
And then :
view.addDialog(new addDialogListener(view));
public class EventController {
}
instead of
public class EventController implements ActionListener {
}
I have a Wicket Panel that contains a ListView and then sub-items (Form controls), but when I press an inner CheckBox, the visibility of some of the sub-items should change.
However, calling WebMarkupContainer.setVisible(false) does not hide the items within the ListView after the ListView is redrawn during the AJAX update.
Code below:
public class ImagePanel extends Panel {
private ArrayList<ImageEntry> imageEntryList;
public class ImageEntry implements Serializable {
private static final long serialVersionUID = -3987685200930059655L;
public String thumbnail;
public String filename;
public boolean webDownloaded;
public WebMarkupContainer fileUpload;
public WebMarkupContainer webDownload;
}
public ImagePanel(String id) {
this(id, IMAGE_NORMAL);
}
public ImagePanel(String id, int type) {
super(id);
this.type = type;
wmc = new WebMarkupContainer ("wmc");
wmc.setOutputMarkupId(true);
add(wmc);
imageEntryList = new ArrayList<ImageEntry>();
ImageEntry imageEntry = new ImageEntry();
imageEntry.thumbnail = "blah";
imageEntry.filename = "blah";
imageEntryList.add(imageEntry);
ListView<ImageEntry> llv = new LargeImageListView("large_image_list", imageEntryList);
wmc.add(llv);
SmallImageListView slv = new SmallImageListView("small_image_list", imageEntryList);
wmc.add(slv);
}
private final class SmallImageListView extends ListView<ImageEntry> {
private SmallImageListView(String id, List<? extends ImageEntry> list) {
super(id, list);
}
#Override
protected void populateItem(final ListItem<ImageEntry> item) {
...
if (type == IMAGE_WIZARD) {
item.getModelObject().fileUpload = showWizardFileUpload(item);
item.getModelObject().webDownload = showWizardWebDownload(item);
showSortUpDown(item);
showWebCheckbox(item);
}
}
}
private void showWebCheckbox(final ListItem<ImageEntry> item) {
AjaxCheckBox checkbox = new AjaxCheckBox("use_web_image", new PropertyModel<Boolean>(item.getModelObject(), "webDownloaded")) {
public void onUpdate(AjaxRequestTarget target) {
if (getModelObject()) {
System.out.println("Show");
item.getModelObject().fileUpload.setVisible(false);
item.getModelObject().webDownload.setVisible(false);
} else {
System.out.println("Hide");
item.getModelObject().fileUpload.setVisible(false);
item.getModelObject().webDownload.setVisible(false);
}
target.add(wmc);
}
};
item.add(checkbox);
}
...
}
Use ListView.setReuseItems(true) to ensure that the objects within the ListView are serialized correctly... Otherwise, you will receive a different object each time and the .setVisible() property will be reset to its default value (e.g. 'true').
slv.setReuseItems(true);
Try
WebMarkupContainer.add(new AttributeModifier("style", new Model("display:none")));
or
WebMarkupContainer.add(new AttributeAppender("style", new Model("display:none"), "="));
instead.
I have a ButtonField on MainScreen, from which I am pushing a PopupScreen where I have added an ObjectListfield. What I want to do is to update the label of ButtonField on MainScreen with the element selected from ObjectListfield of PopupScreen.
Please tell me if it is possible to do without using Dialog class (I really want to use PopupScreen and not Dialog class) and the method by which this can be done. I'd appreciate if some sample code will be provided.
I have added my code.
public final class MyScreen extends MainScreen {
HorizontalFieldManager hfm;
ButtonField btn;
public MyScreen() {
// Set the displayed title of the screen
super();
setTitle("MyTitle");
btn = new ButtonField("label",ButtonField.CONSUME_CLICK);
final mypopup mps = new mypopup();
btn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field,int context) {
UiApplication.getUiApplication().pushModalScreen(mps);
}
});
hfm = new HorizontalFieldManager();
hfm.add(btn);
add(hfm);
}
public void setlabel(String labelnew) {
btn.setLabel(labelnew);
}
public String getlabel() {
return this.btn.getLabel();
}
}
class mypopup extends PopupScreen implements FieldChangeListener {
String it;
ObjectListField obj = new ObjectListField() {
public boolean navigationClick(int status,int time) {
int selectedindex=obj.getSelectedIndex();
it=(String)obj.get(obj, selectedindex);
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
/*MyScreen my=new MyScreen();
my.btn.setLabel(it);
my.invalidate(); */
//close();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
/* This im doing to see that setlabel and getlabel are
working properly */
MyScreen my=new MyScreen();
my.setlabel(it);
String gt=my.getlabel();
Dialog.alert(gt);
my.hfm.invalidate();
//the label of button is changed but not updating in mainscreen.
}
});
return true;
}
};
public mypopup() {
super(new VerticalFieldManager());
String[] type=new String[] {"a","b","c","d"};
obj.set(type);
add(obj);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
}
}
You need to change following block of code,
MyScreen my = new MyScreen();
my.setlabel(it);
String gt = my.getlabel();
Dialog.alert(gt);
my.hfm.invalidate();
With the code block,
Screen scr = UiApplication.getUiApplication().getActiveScreen();
if (scr instanceof MyScreen) {
MyScreen my = (MyScreen) scr;
my.setlabel(it);
my.invalidate();
}
Add the button in one Manager either HorizontalFieldManager or VerticalFieldManager and after setting text on button invalidate the managerlike this way
public final class MyScreen extends MainScreen
{
ButtonField btn;
public MyScreen()
{
// Set the displayed title of the screen
super();
setTitle("MyTitle");
btn=new ButtonField("label",ButtonField.CONSUME_CLICK);
final mypopup mps=new mypopup();
btn.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field,int context){
UiApplication.getUiApplication().pushModalScreen(mps);
}
});
HorizontalFieldManager hfmToholdButtons = new HorizontalFieldManager();
btn.setLabel(mps.gettext());
hfmToholdButtons.add(btn);
hfmToholdButtons.invalidate();
}
}
I have played for a while with History in gwt because i intend to implement it in my current project, so i created a small demo project just to see how it works and do some practice.
So, for some reasons it does not work.
Here is the code - very simple back and forward.
here is the iframe
<iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
then the entry point
public class EntryPoint implements com.google.gwt.core.client.EntryPoint {
private FirstPanel panel;
public void onModuleLoad() {
ContentPanel panel = ContentPanel.getInstance();
panel.setContent(FirstPanel.getInstance());
RootPanel.get().add(panel);
}
}
and 3 singleton forms, content where form are being loaded and 2 form.
public class ContentPanel extends Composite
{
private static ContentPanel instance;
private static VerticalPanel panel = new VerticalPanel();
private ContentPanel()
{
initWidget(panel);
}
public void setContent(Widget widget)
{
panel.clear();
panel.add(widget);
}
public static ContentPanel getInstance()
{
if(instance == null)
return instance = new ContentPanel();
else
return instance;
}
}
and ..
public class FirstPanel extends Composite implements HistoryListener {
private static FirstPanel instance;
private VerticalPanel panel;
public FirstPanel() {
History.addHistoryListener(this);
panel = new VerticalPanel();
panel.setStyleName("panelstyle");
Button button2 = new Button("Next page");
button2.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
History.newItem("second_page");
}
});
panel.add(button2);
initWidget(panel);
}
public static FirstPanel getInstance()
{
if(instance == null)
return instance = new FirstPanel();
else
return instance;
}
public Widget getWidget() {
return panel;
}
public void onHistoryChanged(String historyToken) {
if(historyToken.equalsIgnoreCase("second_page"))
ContentPanel.getInstance().setContent(SecondPanel.getInstance());
}
}
and the last but not least :))
public class SecondPanel extends Composite implements HistoryListener
{
private static SecondPanel instance;
public SecondPanel()
{
History.addHistoryListener(this);
VerticalPanel verticalPanel = new VerticalPanel();
TextBox firstnameBox = new TextBox();
TextBox lastnameBox = new TextBox();
Button submitButton = new Button("Click");
verticalPanel.add(firstnameBox);
verticalPanel.add(lastnameBox);
verticalPanel.add(submitButton);
submitButton.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
History.newItem("first_panel");
alert("You are in second panel");
}
});
initWidget(verticalPanel);
}
public static SecondPanel getInstance()
{
if(instance == null)
return instance = new SecondPanel();
else
return instance;
}
public void onHistoryChanged(String historyToken)
{
if(historyToken.equalsIgnoreCase("first_panel"))
ContentPanel.getInstance().setContent(FirstPanel.getInstance());
}
}
The problem is that i click the button in FirstPanel and SecandPanel is loaded then press "Back" in the browser does not do anything.In onHistoryChanged method the param historyToken is empty string, should't be a value from the stack (first_page)?
I will be grateful if someone find the problem or explain me where i do mistakes.
Thanks
On first page load you are calling onHistoryChanged(INIT_STATE) by hand. This does not change history. Replace with this:
public FirstPanel() {
History.addHistoryListener(this);
String token = History.getToken();
if (token.length() == 0) {
History.newItem(INIT_STATE);
} else {
History.fireCurrentHistoryState();
}
.. rest of code
Better practice would be to register History listeners History.addHistoryListener(..) only in the top-most panel (EntryPoint or ContentPanel) and switch panels based on history from there.