PageRenderTime 138ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/com.plugin.log4j.viewer/src/com/log4jviewer/ui/preferences/filters/FilterController.java

https://code.google.com/p/log4j-viewer/
Java | 180 lines | 145 code | 28 blank | 7 comment | 53 complexity | f53dafd225bb73a36a83262e706cb58d MD5 | raw file
  1. package com.log4jviewer.ui.preferences.filters;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import javax.xml.parsers.ParserConfigurationException;
  5. import org.eclipse.jface.preference.IPreferenceStore;
  6. import org.eclipse.jface.viewers.ColumnViewerEditor;
  7. import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
  8. import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
  9. import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
  10. import org.eclipse.jface.viewers.ISelectionChangedListener;
  11. import org.eclipse.jface.viewers.SelectionChangedEvent;
  12. import org.eclipse.jface.viewers.TableViewer;
  13. import org.eclipse.jface.viewers.TableViewerEditor;
  14. import org.eclipse.jface.viewers.TableViewerFocusCellManager;
  15. import org.eclipse.swt.SWT;
  16. import org.eclipse.swt.events.MouseEvent;
  17. import org.eclipse.swt.graphics.Point;
  18. import org.eclipse.swt.graphics.Rectangle;
  19. import org.eclipse.swt.widgets.Table;
  20. import org.eclipse.swt.widgets.TableItem;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import com.log4jviewer.filters.ConfigFilterSaver;
  24. import com.log4jviewer.filters.FilterItemModel;
  25. import com.log4jviewer.filters.FilterModel;
  26. import com.log4jviewer.ui.preferences.DefaultPreferences;
  27. /**
  28. * Controller for filters' preferences. It's a channel between filter's and filter items' ui components.
  29. *
  30. * @authors <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko </a></br><a
  31. * href="mailto:Daniil.Yaroslavtsev@gmail.com">Daniil Yaroslavtsev</a>
  32. */
  33. public class FilterController {
  34. private final Logger logger = LoggerFactory.getLogger(getClass());
  35. private FilterRuleSettings filterRuleSettings;
  36. public void setFilterRuleSettings(final FilterRuleSettings filterRuleSettings) {
  37. this.filterRuleSettings = filterRuleSettings;
  38. }
  39. public void saveFilters(final String filtersConfigFilePath, final List<FilterModel> filterModels)
  40. throws IOException,
  41. ParserConfigurationException {
  42. ConfigFilterSaver filterSaver = new ConfigFilterSaver();
  43. filterSaver.saveFilters(filtersConfigFilePath, filterModels);
  44. logger.debug("Filters were saved.");
  45. }
  46. public void saveActiveFilter(final IPreferenceStore store, final int activeFilterIndex) {
  47. store.setValue(DefaultPreferences.FilterPreferences.getId(), activeFilterIndex);
  48. logger.debug("Active filter with index = {} was saved.", activeFilterIndex);
  49. }
  50. public void removeItemsFromFilterRuleTable() {
  51. filterRuleSettings.getTableViewer().getTable().removeAll();
  52. filterRuleSettings.checkAndSetAddButtonState();
  53. }
  54. public void addFilterTableListener(final TableViewer filterTableViewer,
  55. final FilterContentProvider filterContentProvider) {
  56. filterTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  57. @Override
  58. public void selectionChanged(final SelectionChangedEvent event) {
  59. TableViewer filterRuleTableViewer = filterRuleSettings.getTableViewer();
  60. filterRuleTableViewer.getTable().removeAll();
  61. int selectedFilterIndex = filterTableViewer.getTable().getSelectionIndex();
  62. if (selectedFilterIndex >= 0) {
  63. filterContentProvider.setSelectedFilterIndex(selectedFilterIndex);
  64. List<FilterItemModel> filterItemModels = filterContentProvider
  65. .getFilterItems(selectedFilterIndex);
  66. filterRuleTableViewer.setInput(filterItemModels);
  67. }
  68. filterRuleSettings.checkAndSetAddButtonState();
  69. logger.debug("Index of selected filter = {}", selectedFilterIndex);
  70. }
  71. });
  72. }
  73. public void setCellEditingStrategy(final TableViewer tableViewer) {
  74. TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tableViewer,
  75. new FocusCellOwnerDrawHighlighter(tableViewer));
  76. ColumnViewerEditorActivationStrategy activationStrategy =
  77. new ColumnViewerEditorActivationStrategy(tableViewer) {
  78. @Override
  79. protected boolean isEditorActivationEvent(
  80. final ColumnViewerEditorActivationEvent event) {
  81. boolean result = false;
  82. Table table = tableViewer.getTable();
  83. int selectedCellIndex = getSelectedCellIndex(event, table);
  84. if (table.getColumnCount() == FilterSettings.COLUMN_COUNT) {
  85. result = getFilterTableActivationStrategy(event, selectedCellIndex);
  86. } else if (table.getColumnCount() == FilterRuleSettings.COLUMN_NUMBER) {
  87. result = getFilterRuleTableActivationStrategy(event, selectedCellIndex);
  88. }
  89. return result;
  90. }
  91. };
  92. TableViewerEditor.create(tableViewer, focusCellManager, activationStrategy,
  93. ColumnViewerEditor.TABBING_HORIZONTAL
  94. | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
  95. | ColumnViewerEditor.TABBING_VERTICAL
  96. | ColumnViewerEditor.KEYBOARD_ACTIVATION);
  97. }
  98. private boolean getFilterTableActivationStrategy(final ColumnViewerEditorActivationEvent evt,
  99. final int selectedCellIndex) {
  100. boolean result = false;
  101. if (selectedCellIndex == FilterSettings.ENABLE_COLUMN_INDEX) {
  102. result = (evt.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION);
  103. } else {
  104. result = (evt.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL)
  105. || (evt.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION)
  106. || ((evt.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED) && (evt.keyCode == SWT.CR))
  107. || ((evt.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED) && (evt.keyCode == SWT.F2))
  108. || ((evt.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED)
  109. && (evt.keyCode == SWT.KEYPAD_CR))
  110. || ((evt.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED)
  111. && (evt.keyCode > 31) && (evt.keyCode < 127))
  112. || ((evt.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED)
  113. && (evt.keyCode >= SWT.KEYPAD_MULTIPLY) && (evt.keyCode <= SWT.KEYPAD_9))
  114. || (evt.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC);
  115. }
  116. return result;
  117. }
  118. private boolean getFilterRuleTableActivationStrategy(final ColumnViewerEditorActivationEvent evt,
  119. final int selectedCellIndex) {
  120. boolean result = false;
  121. if (selectedCellIndex == FilterRuleSettings.ENABLE_COLUMN_INDEX) {
  122. result = (evt.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION);
  123. } else {
  124. result = (evt.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL)
  125. || ((evt.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED) && (evt.keyCode == SWT.F2))
  126. || (evt.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION)
  127. || (evt.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC);
  128. }
  129. return result;
  130. }
  131. // Looking for selected cell index in table item
  132. private int getSelectedCellIndex(final ColumnViewerEditorActivationEvent event,
  133. final Table table) {
  134. int cellIndex = -1;
  135. if (event.sourceEvent instanceof MouseEvent) {
  136. int x = ((MouseEvent) event.sourceEvent).x;
  137. int y = ((MouseEvent) event.sourceEvent).y;
  138. Point clickLocation = new Point(x, y);
  139. TableItem tableItem = table.getItem(clickLocation);
  140. if (tableItem != null) {
  141. for (int i = 0; i < table.getColumnCount(); i++) {
  142. Rectangle rect = tableItem.getBounds(i);
  143. if (rect.contains(clickLocation)) {
  144. cellIndex = i;
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. return cellIndex;
  151. }
  152. }