PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/google/security/zynamics/binnavi/Gui/MainWindow/ProjectTree/Nodes/Data/Component/TypeInstanceDialog.java

https://gitlab.com/Manouchehri/binnavi
Java | 263 lines | 194 code | 27 blank | 42 comment | 18 complexity | 109f1d6293fca24a56a0ab2b0fc2fcfa MD5 | raw file
  1. /*
  2. Copyright 2011-2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.google.security.zynamics.binnavi.Gui.MainWindow.ProjectTree.Nodes.Data.Component;
  14. import com.google.common.base.Preconditions;
  15. import com.google.security.zynamics.binnavi.Gui.GraphWindows.types.TypeComboBox;
  16. import com.google.security.zynamics.binnavi.Gui.GraphWindows.types.TypeListModel;
  17. import com.google.security.zynamics.binnavi.disassembly.types.BaseType;
  18. import com.google.security.zynamics.binnavi.disassembly.types.Section;
  19. import com.google.security.zynamics.binnavi.disassembly.types.TypeInstance;
  20. import com.google.security.zynamics.binnavi.disassembly.types.TypeManager;
  21. import com.google.security.zynamics.zylib.gui.CDialogEscaper;
  22. import com.google.security.zynamics.zylib.gui.CMessageBox;
  23. import java.awt.BorderLayout;
  24. import java.awt.FlowLayout;
  25. import java.awt.GridBagConstraints;
  26. import java.awt.GridBagLayout;
  27. import java.awt.Insets;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import javax.swing.JButton;
  31. import javax.swing.JDialog;
  32. import javax.swing.JFrame;
  33. import javax.swing.JLabel;
  34. import javax.swing.JPanel;
  35. import javax.swing.JTextField;
  36. /**
  37. * Dialog to create a new or edit an existing type instance.
  38. */
  39. public class TypeInstanceDialog extends JDialog {
  40. private static final long serialVersionUID = -7912640983889588677L;
  41. private JTextField instanceName;
  42. private TypeComboBox types;
  43. private boolean okClicked;
  44. private JTextField sectionOffset;
  45. private final Section section;
  46. private static Long sanitizedOffset;
  47. private TypeInstanceDialog(final JFrame owner,
  48. final String dialogTitle,
  49. final TypeListModel model,
  50. final TypeInstance instance,
  51. final Section section,
  52. final Long offset) {
  53. super(owner, dialogTitle, true);
  54. this.section = section;
  55. createControls(model);
  56. populateControls(instance, offset);
  57. }
  58. private static boolean isValidSectionOffset(final JTextField textField, final Section section) {
  59. Preconditions.checkNotNull(textField, "Error: textField argument can not be null");
  60. Preconditions.checkNotNull(section, "Error: section argument can not be null");
  61. if (textField.getText().isEmpty()) {
  62. return false;
  63. }
  64. try {
  65. String textFieldString = textField.getText().toLowerCase();
  66. boolean hex = false;
  67. if (textFieldString.startsWith("0x")) {
  68. textFieldString = textFieldString.substring(2);
  69. hex = true;
  70. }
  71. final Long offset = textFieldString.matches("^[0-9A-Fa-f]+$") && hex == true ? Long.parseLong(
  72. textFieldString, 16)
  73. : Long.parseLong(textFieldString, 10);
  74. if (section.isValidOffset(offset)) {
  75. sanitizedOffset = offset;
  76. return true;
  77. }
  78. if (section.isValidAddress(offset)) {
  79. sanitizedOffset = offset - section.getStartAddress().toLong();
  80. return true;
  81. }
  82. return false;
  83. } catch (final NumberFormatException exception) {
  84. return false;
  85. }
  86. }
  87. /**
  88. * Creates a new instance of the type instance dialog in order to create a new type instance. The
  89. * dialog doesn't actually create a new type instance in the type system, instead it provides
  90. * validated user input.
  91. *
  92. * @param owner The window which owns the instantiated dialog.
  93. * @param typeManager The type manager that holds the current type system.
  94. * @param section The section where the type instance should be created.
  95. * @param offset The section relative offset of the type instance. The user needs to enter a valid
  96. * offset in the dialog if this value is null.
  97. * @return A new instance of the dialog.
  98. */
  99. public static TypeInstanceDialog instantiateCreateTypeInstanceDialog(final JFrame owner,
  100. final TypeManager typeManager, final Section section, final Long offset) {
  101. return new TypeInstanceDialog(owner,
  102. "Create type instance",
  103. new TypeListModel(typeManager.getTypes(), new TypeListModel.PrototypesFilter()),
  104. null,
  105. section,
  106. offset);
  107. }
  108. /**
  109. * Creates a new instance of the type instance dialog in order to edit an existing type instance.
  110. * The dialog doesn't actually perform the changes on the type instance, instead it provides
  111. * validated user input.
  112. *
  113. * @param owner The window which owns the instantiated dialog.
  114. * @param typeManager The type manager that holds the current type system.
  115. * @param instance The existing type instance.
  116. * @return A new instance of the dialog.
  117. */
  118. public static TypeInstanceDialog instantiateEditTypeInstanceDialog(final JFrame owner,
  119. final TypeManager typeManager, final TypeInstance instance) {
  120. final TypeInstanceDialog dialog = new TypeInstanceDialog(owner,
  121. "Edit type instance",
  122. new TypeListModel(typeManager.getTypes(), new TypeListModel.PrototypesFilter()),
  123. instance,
  124. instance.getSection(),
  125. instance.getAddress().getOffset());
  126. // For now, we only allow the user to change the name of the type instance
  127. // so we disable all other controls.
  128. dialog.types.setEnabled(false);
  129. dialog.sectionOffset.setEnabled(false);
  130. return dialog;
  131. }
  132. private void createControls(final TypeListModel model) {
  133. final JPanel panel = new JPanel();
  134. getContentPane().add(panel, BorderLayout.SOUTH);
  135. panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  136. final JButton okButton = new JButton("OK");
  137. okButton.addActionListener(new ActionListener() {
  138. @Override
  139. public void actionPerformed(final ActionEvent e) {
  140. if (validateUserInput()) {
  141. okClicked = true;
  142. dispose();
  143. }
  144. }
  145. });
  146. panel.add(okButton);
  147. final JButton cancelButton = new JButton("Cancel");
  148. cancelButton.addActionListener(new ActionListener() {
  149. @Override
  150. public void actionPerformed(final ActionEvent e) {
  151. dispose();
  152. }
  153. });
  154. panel.add(cancelButton);
  155. types = new TypeComboBox(model);
  156. getContentPane().add(types, BorderLayout.CENTER);
  157. final JPanel panel_1 = new JPanel();
  158. getContentPane().add(panel_1, BorderLayout.NORTH);
  159. final GridBagLayout gbl_panel_1 = new GridBagLayout();
  160. gbl_panel_1.columnWidths = new int[] {166, 109, 0};
  161. gbl_panel_1.rowHeights = new int[] {0, 0, 15, 0};
  162. gbl_panel_1.columnWeights = new double[] {0.0, 1.0, Double.MIN_VALUE};
  163. gbl_panel_1.rowWeights = new double[] {0.0, 0.0, 0.0, Double.MIN_VALUE};
  164. panel_1.setLayout(gbl_panel_1);
  165. final JLabel lblInstanceName_1 = new JLabel("Instance name:");
  166. final GridBagConstraints gbc_lblInstanceName_1 = new GridBagConstraints();
  167. gbc_lblInstanceName_1.anchor = GridBagConstraints.WEST;
  168. gbc_lblInstanceName_1.insets = new Insets(0, 0, 5, 5);
  169. gbc_lblInstanceName_1.gridx = 0;
  170. gbc_lblInstanceName_1.gridy = 0;
  171. panel_1.add(lblInstanceName_1, gbc_lblInstanceName_1);
  172. instanceName = new JTextField();
  173. final GridBagConstraints gbc_instanceName = new GridBagConstraints();
  174. gbc_instanceName.insets = new Insets(0, 0, 5, 0);
  175. gbc_instanceName.fill = GridBagConstraints.HORIZONTAL;
  176. gbc_instanceName.gridx = 1;
  177. gbc_instanceName.gridy = 0;
  178. panel_1.add(instanceName, gbc_instanceName);
  179. instanceName.setColumns(10);
  180. final JLabel lblSectionOffset = new JLabel("Section offset:");
  181. final GridBagConstraints gbc_lblSectionOffset = new GridBagConstraints();
  182. gbc_lblSectionOffset.anchor = GridBagConstraints.WEST;
  183. gbc_lblSectionOffset.insets = new Insets(0, 0, 5, 5);
  184. gbc_lblSectionOffset.gridx = 0;
  185. gbc_lblSectionOffset.gridy = 1;
  186. panel_1.add(lblSectionOffset, gbc_lblSectionOffset);
  187. sectionOffset = new JTextField();
  188. final GridBagConstraints gbc_sectionOffset = new GridBagConstraints();
  189. gbc_sectionOffset.insets = new Insets(0, 0, 5, 0);
  190. gbc_sectionOffset.fill = GridBagConstraints.HORIZONTAL;
  191. gbc_sectionOffset.gridx = 1;
  192. gbc_sectionOffset.gridy = 1;
  193. panel_1.add(sectionOffset, gbc_sectionOffset);
  194. sectionOffset.setColumns(10);
  195. pack();
  196. new CDialogEscaper(this);
  197. }
  198. private void populateControls(final TypeInstance instance, final Long offset) {
  199. if (instance != null) {
  200. instanceName.setText(instance.getName());
  201. types.getModel().selectByBaseType(instance.getBaseType());
  202. }
  203. if (offset != null) {
  204. sectionOffset.setText(String.valueOf(offset));
  205. }
  206. }
  207. private boolean validateUserInput() {
  208. if (getInstanceType() == null || getInstanceName() == null) {
  209. CMessageBox.showWarning(this,
  210. "Please enter a name for the type instance and select a base type.");
  211. return false;
  212. } else if (!isValidSectionOffset(sectionOffset, section)) {
  213. CMessageBox.showWarning(this, String.format(
  214. "Please enter a valid section offset!\n The valid range for '%s' is [0x%X-0x%X).",
  215. section.getName(), section.getStartAddress().toLong(), section.getEndAddress().toLong()));
  216. return false;
  217. } else {
  218. return true;
  219. }
  220. }
  221. public String getInstanceName() {
  222. return instanceName.getText();
  223. }
  224. public BaseType getInstanceType() {
  225. return (BaseType) types.getSelectedItem();
  226. }
  227. public long getSectionOffset() {
  228. return sanitizedOffset;
  229. }
  230. public boolean wasOkClicked() {
  231. return okClicked;
  232. }
  233. }