blob: 23a8a832ac03448400065126b471b0ee9c60d409 [file] [log] [blame]
/**
* Copyright (c) 2011, 2014 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.vaaclipse.addons.keybinding.view;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import org.eclipse.core.commands.CommandManager;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MBindingContext;
import org.eclipse.e4.ui.model.application.commands.MBindingTable;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MKeyBinding;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.vaaclipse.addons.common.api.IE4Topics;
import org.eclipse.osbp.vaaclipse.addons.keybinding.IKeyBindingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
/**
* View that shows problems.
*/
public class KeybindingView {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(KeybindingView.class);
/** The parent. */
private final VerticalLayout parent;
/** The eclipse context. */
@SuppressWarnings("unused")
private final IEclipseContext eclipseContext;
/** The command manager. */
@Inject
private CommandManager commandManager;
/** The i18n service. */
@Inject
private II18nService i18nService;
/** The keybinding service. */
@Inject
private IKeyBindingService keybindingService;
/** The table. */
private Table table;
/** The container. */
private BeanItemContainer<BindingBean> container;
/**
* Instantiates a new keybinding view.
*
* @param parent
* the parent
* @param eclipseContext
* the eclipse context
* @param app
* the app
*/
@Inject
public KeybindingView(VerticalLayout parent,
IEclipseContext eclipseContext, MApplication app) {
this.parent = parent;
this.eclipseContext = eclipseContext;
}
/**
* Inits the.
*/
@SuppressWarnings("serial")
@PostConstruct
protected void init() {
table = new Table();
table.setSelectable(true);
table.setSizeFull();
parent.addComponent(table);
container = new BeanItemContainer<BindingBean>(BindingBean.class);
table.setContainerDataSource(container);
List<MKeyBinding> bindings = keybindingService.getAllKeyBindings();
for (MKeyBinding binding : bindings) {
container.addBean(new BindingBean(binding));
}
table.setVisibleColumns(new Object[] { "name", "description",
"keySequence", "category", "contextName", "contextDescription" });
table.setColumnCollapsingAllowed(true);
table.setColumnCollapsed("category", true);
table.setColumnCollapsed("contextDescription", true);
Locale locale = UI.getCurrent().getLocale();
table.setColumnHeader("name", toViewI18n("name", i18nService, locale));
table.setColumnHeader("description",
toViewI18n("description", i18nService, locale));
table.setColumnHeader("category",
toViewI18n("category", i18nService, locale));
table.setColumnHeader("keySequence",
toViewI18n("keySequence", i18nService, locale));
table.setColumnHeader("contextName",
toViewI18n("contextName", i18nService, locale));
table.setColumnHeader("contextDescription",
toViewI18n("contextDescription", i18nService, locale));
table.addItemClickListener(new ItemClickEvent.ItemClickListener() {
@Override
public void itemClick(ItemClickEvent event) {
if (event.isDoubleClick()) {
}
}
});
table.addValueChangeListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
// showDetailMessage((StatusBean)
// event.getProperty().getValue());
}
});
// parent.setExpandRatio(table, 0.85f);
}
/**
* To view i18n.
*
* @param key
* the key
* @param i18nService
* the i18n service
* @param locale
* the locale
* @return the string
*/
private static String toViewI18n(String key, II18nService i18nService,
Locale locale) {
String result = i18nService.getValue(
"org.eclipse.osbp.vaaclipse.addons.keybinding.view.KeybindingView."
+ key, locale);
if (result == null || result.equals("")) {
return key;
}
return result;
}
/**
* To common i18n.
*
* @param key
* the key
* @param i18nService
* the i18n service
* @param locale
* the locale
* @return the string
*/
@SuppressWarnings("unused")
private static String toCommonI18n(String key, II18nService i18nService,
Locale locale) {
if (key == null) {
return "";
}
String result = i18nService.getValue(key, locale);
if (result == null || result.equals("")) {
return key;
}
return result;
}
/**
* Creates an event to focus a field in a MPart.
*
* @param mPartId
* the m part id
* @param fieldId
* the field id
* @return the map
*/
protected Map<String, Object> createFocusFieldEvent(String mPartId,
String fieldId) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(IE4Topics.PartEvents.PROP_MPART_ID, mPartId);
properties.put(IE4Topics.PartEvents.PROP_FIELD_ID, fieldId);
return properties;
}
/**
* Dispose.
*/
@PreDestroy
protected void dispose() {
table = null;
container = null;
}
/**
* The Class BindingBean.
*/
public static class BindingBean {
/** The binding. */
private MKeyBinding binding;
/** The binding table. */
private MBindingTable bindingTable;
/** The binding context. */
private MBindingContext bindingContext;
/** The command. */
private MCommand command;
/**
* Instantiates a new binding bean.
*
* @param binding
* the binding
*/
public BindingBean(MKeyBinding binding) {
super();
this.binding = binding;
this.command = binding.getCommand();
this.bindingTable = (MBindingTable) ((EObject) binding)
.eContainer();
this.bindingContext = bindingTable.getBindingContext();
}
/**
* Gets the description.
*
* @return the description
* @throws NotDefinedException
* the not defined exception
*/
public final String getDescription() throws NotDefinedException {
return command.getDescription();
}
/**
* Gets the name.
*
* @return the name
* @throws NotDefinedException
* the not defined exception
*/
public final String getName() throws NotDefinedException {
return command.getCommandName();
}
/**
* Gets the id.
*
* @return the id
*/
public final String getId() {
return command.getElementId();
}
/**
* Gets the category.
*
* @return the category
* @throws NotDefinedException
* the not defined exception
*/
public final String getCategory() throws NotDefinedException {
try {
return command.getCategory().getName();
} catch (Exception e) {
return "";
}
}
/**
* Gets the key sequence.
*
* @return the key sequence
*/
public final String getKeySequence() {
return binding.getKeySequence();
}
/**
* Gets the context name.
*
* @return the context name
*/
public final String getContextName() {
return bindingContext.getName();
}
/**
* Gets the context description.
*
* @return the context description
*/
public final String getContextDescription() {
return bindingContext.getDescription();
}
}
}