Open pdf file from menubar using Vaadin - java

I have a menubar in my vaadin application and want to add an item to open a pdf-file is a new tab of the browser. I found some solutions to open files with a button, but I have to use an MenuItem...
MenuBar.Command commandHandler = new MenuBar.Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
if (selectedItem.equals(menu_help)) {
openHelp();
}
}
};
...
menu_help = menuBar
.addItem("", WebImageList.getImage(ImageList.gc_helpIcon),
commandHandler);
...
private void openHelp() {
// open pdf-file in new window
}
Thanks for help!
SOLUTION:
private void openHelp() {
final String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
Resource pdf = new FileResource(new File(basepath + "/WEB-INF/datafiles/help.pdf"));
setResource("help", pdf);
ResourceReference rr = ResourceReference.create(pdf, this, "help");
Page.getCurrent().open(rr.getURL(), "blank_");
}
Attention: This code works, but the the structure of code is not perfect ;-) Better is to store "basepath" and "pdf" as attribute...

There is a similar problem described here: How to specify a button to open an URL?
One possible solution:
public class MyMenuBar extends MenuBar {
ResourceReference rr;
public MyMenuBar() {
Resource pdf = new FileResource(new File("C:/temp/temp.pdf"));
setResource("help", pdf);
rr = ResourceReference.create(pdf, this, "help");
}
private void openHelp() {
Page.getCurrent().open(rr.getURL(), "blank_");
}
...
}
The setResource method of AbstractClientConnector is protected, so this is you need to extend some Vaadin component to make it work. This is why Im creating the class MyMenuBar here. If you are using an external resource you don't need to attach it to any component with setResource and then this is not nessecary.

I used the following code to do something similar:
private Component buildUserMenu() {
final MenuBar settings = new MenuBar();
settings.addStyleName("user-menu");
final User user = getCurrentUser();
settingsItem = settings.addItem("", new ThemeResource(
"img/logo.png"), null);
updateUserName(null);
settingsItem.addItem(Lang.getMessage("menu.edit"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
ProfilePreferencesWindow.open(user, false);
}
});
settingsItem.addSeparator();
settingsItem.addItem(Lang.getMessage("menu.help"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
Window help = new Window();
help.setWidth("90%");
help.setHeight("90%");
BrowserFrame e = new BrowserFrame("PDF File", new ThemeResource("pdf/ayuda.pdf"));
e.setWidth("100%");
e.setHeight("100%");
help.setContent(e);
help.center();
help.setModal(true);
UI.getCurrent().addWindow(help);
}
});
settingsItem.addSeparator();
settingsItem.addItem(Lang.getMessage("menu.logout"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
BarsEventBus.post(new UserLoggedOutEvent());
}
});
return settings;
}

Related

How to tell a FieldEditorPreferencePage to invalidate/redraw programatically?

I defined a custom FieldEditorPreferencePage. In createFieldEditors method I construct a simple interface with a custom FieldEditor that holds button that loads a file. When a user loads the file the interface region (where the button stands) need to change and show other specific gui. How to invalidate the FieldEditorPreferencePage? I used the layout() method of the parent composite but nothing happens. What am I doing wrong here?
public class MyPage extends FieldEditorPreferencePage {
...
private Composite fieldEditorParent;
private boolean fileLoaded = false;
#Override
protected void createFieldEditors() {
fieldEditorParent = this.getFieldEditorParent();
MyFieldEditor compositeFieldEditor = new MyFieldEditor(fieldEditorParent) {
if(fileLoaded) {
//draw file content
} else {
Button chooseButton = new Button(buttonsComposite, SWT.NONE);
chooseButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//choose file
fileLoaded = true;
//invalidate page <-- THIS DOES NOT WORK!
fieldEditorParent.layout(true, true);
}
}
}
}
}

Can't Use System Menubar Javafx

I want to add a JavaFX Menu Bar to a Stage, but have it use the System Menubar for Mac.
My problem is that using:
menuBar.useSystemMenuBarProperty().set(true);
does not work. I believe that the problem is because my main method isn't part of a JavaFX Application. My main method looks like this:
public class SURPG_Launcher {
public static com.apple.eawt.Application macApp;
public static void main(String[] args) {
if(Toolbox.isMac()) {
initMac(args);
} else {
Application.launch(SURPG_Main.class, args);
}
}
private static void initMac(String[] args) {
System.out.println("MacOS System detected!");
macApp = com.apple.eawt.Application.getApplication();
macApp.setPreferencesHandler(new PreferencesHandler(){
#Override
public void handlePreferences(PreferencesEvent arg0) {
Platform.runLater(() -> {
Stage prefs = new Stage();
prefs.setMinHeight(200);
prefs.setMinWidth(200);
prefs.show();
});
}
});
Application.launch(SURPG_Mac.class, args);
}
}
SURPG_Mac.class and SURPG_Main.class are classes that extend the JavaFX Application.
I have another class that sets the GUI, a stage with a BorderPane. I have another class with public static methods that can be called to set the Menubars, as such:
public class MenuControl {
public static MenuBar menuBar;
public static Menu menuFile;
public static Menu menuGame;
public static Menu menuTools;
public static MenuItem save;
public static void initMenusMac() {
menuBar = new MenuBar();
Platform.runLater(() -> {
menuBar.useSystemMenuBarProperty().set(true);
});
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
public static void initMenus() {
menuBar = new MenuBar();
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
}
My final point is that I CANNOT change it so the main method is in either SURPG_Mac or SURPG_Main, due to a different compatibility problem with Mac integration.
Can anyone help me with this?
Thank you so much in advance!
Have a look at this probject: https://github.com/codecentric/NSMenuFX It allows you to have a more mac-like menu bar. But you will probably have to clean up your somewhat strange project setup as well before you can use it.

Multipage: Navigator & Navigation setup -> "Component must be attached to a session when getConnectorId() is called for the first time"

i cannot get a navigator to run within my project.
i always get the Error
"Component must be attached to a session when getConnectorId() is called for the first time"
i followed several examples where it ran out of the box with nealy the exact same setup but i cannot get it to run within my environment and i don't know why.
This is my MainUI
#SpringUI
public class MainUI extends UI implements View{
HorizontalLayout root;
#Override
protected void init(VaadinRequest request) {
root = new HorizontalLayout();
VerticalLayout menu = this.createMenuBar();
menu.setWidth("20%");
root.addComponents(menu);
Panel viewPanel = new Panel();
root.addComponentsAndExpand(viewPanel);
setContent(root);
final Navigator navigator = new Navigator(this, viewPanel);
navigator.addView("Food", FoodView.class);
navigator.addView("Meal", MealView.class);
navigator.addView("", MainView.class);
// even with
//navigator.addView("Main", MainView.class);
//navigator.navigateTo("Main");
// i get the same error
}
#Override
public void enter(ViewChangeEvent event) {
// TODO
}
private VerticalLayout createMenuBar() {
VerticalLayout menuBar = new VerticalLayout();
for(String s: new String[] {"Food", "Meal"}) {
menuBar.addComponents(this.createNavigationButton(s, this.getNavigator()));
}
return menuBar;
}
private Button createNavigationButton(final String state, final Navigator navigator) {
return new Button("Go go" + state, new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
navigator.navigateTo(state);
}
});
}
}
This is my Main/Food/Meal View, they are only stumps with only a label because i just wanted to test a Multipage example
#SpringView(name = MainView.VIEW_NAME)
public class MainView extends UI implements View {
public static final String VIEW_NAME = "Main";
#Override
protected void init(VaadinRequest request) {
VerticalLayout root = new VerticalLayout();
setContent(root);
Label lbl = new Label("MainView");
root.addComponent(lbl);
}
#Override
public void enter(ViewChangeEvent event) {
}
}

How to set image datasource in vaadin

I have a horizontal split panel in which I want to show the image selected in combobox but unable to set the datasource for image file.
FilesystemContainer container = new FilesystemContainer(new File("C:/myData/wallpaper"));
ComboBox box = new ComboBox("Documents", container);
#Override
protected void init(VaadinRequest request) {
setContent(box);
com.vaadin.ui.HorizontalSplitPanel horizontalSplitPanel = new com.vaadin.ui.HorizontalSplitPanel();
setContent(horizontalSplitPanel);
horizontalSplitPanel.addComponent(box);
//horizontalSplitPanel.addComponent(label);
final Image image = new Image();
horizontalSplitPanel.addComponent(image);
box.addValueChangeListener(new ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
image.setData(event.getProperty().getValue());
///label.set//setPropertyDataSource( (Property) ImageIO.read((ImageInputStream) new TextFileProperty((File) event.getProperty().getValue())));
}
});
box.setImmediate(true);
How can I set datasource for images.I'm very new in Vaadin.
I suggest this way:
#Override
public void valueChange(ValueChangeEvent event) {
image.setSource(new FileResource((File)box.getValue()));
}

java program flow on example of DynamicReports and progressbar

i use DynamicReports library for make reports for my app. Createing report takes some time and i decided create custom progress bar while reports has not been created. Question after code examples.
progress bar class:
public class ProgressDialog implements DialogWrapper{
private JFrame iFrame;
private JDialog iDialog;
private JPanel pane;
private final JProgressBar aJProgressBar = new JProgressBar(0, 100);
public ProgressDialog(){
onCreate();
}
#Override
public void onCreate() {
iFrame = new JFrame("Создание отчета");
iDialog = new JDialog(iFrame, true);
pane = new JPanel();
aJProgressBar.setIndeterminate(true);
pane.add(aJProgressBar, BorderLayout.NORTH);
iDialog.add(pane, BorderLayout.CENTER);
iDialog.setTitle("Создание отчета");
iDialog.setSize(300, 150);
iDialog.setResizable(false);
iDialog.setVisible(true);
return;
}
#Override
public void fillData() {}
#Override
public void onSubmit() {}
protected void onCancel(){
iDialog.setVisible(false);
iDialog.dispose();
}
public void cancel(){
onCancel();
}
}
Report abstract class
public abstract class AbstractReportMain<T extends ReportDesign<U>, U extends ReportData> {
private ProgressDialog pd;
public AbstractReportMain() {
pd = new ProgressDialog();
build();
}
protected void build() {
try {
JasperReportBuilder reportBuilder = DynamicReports.report();
U data = getReportData();
if (data != null) {
reportBuilder.setDataSource(data.createDataSource());
}
getReportDesign().configureReport(reportBuilder, data);
pd.cancel();
reportBuilder.show(false);
} catch (DRException e) {
e.printStackTrace();
}
}
protected U getReportData() {
return null;
}
protected abstract T getReportDesign();
}
Question: when i create ProgressDialog, program flow stops while i do not close dialog. Why it's happen, how this behaviour called and where i can read about it? How it use and маке it work for me. Thanks.
I believe your problem is that you are not creating a new thread for your progress dialog. Here is an example that will help you. Modal Progress Dialog example

Categories

Resources