1 /*
2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
5 */
6
7
8 #include "EnumerationValueHandler.h"
9
10 #include <new>
11
12 #include "EnumerationValue.h"
13 #include "EnumerationValueFormatter.h"
14 #include "TableCellEnumerationEditor.h"
15 #include "TableCellFormattedValueRenderer.h"
16 #include "Type.h"
17
18
EnumerationValueHandler()19 EnumerationValueHandler::EnumerationValueHandler()
20 {
21 }
22
23
~EnumerationValueHandler()24 EnumerationValueHandler::~EnumerationValueHandler()
25 {
26 }
27
28
29 status_t
Init()30 EnumerationValueHandler::Init()
31 {
32 return B_OK;
33 }
34
35
36 float
SupportsValue(Value * value)37 EnumerationValueHandler::SupportsValue(Value* value)
38 {
39 return dynamic_cast<EnumerationValue*>(value) != NULL ? 0.7f : 0;
40 }
41
42
43 status_t
GetValueFormatter(Value * _value,ValueFormatter * & _formatter)44 EnumerationValueHandler::GetValueFormatter(Value* _value,
45 ValueFormatter*& _formatter)
46 {
47 EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
48 if (value == NULL)
49 return B_BAD_VALUE;
50
51 IntegerValueFormatter::Config* config = NULL;
52 status_t error = CreateIntegerFormatterConfig(value, config);
53 if (error != B_OK)
54 return error;
55 BReference<IntegerValueFormatter::Config> configReference(config, true);
56
57 ValueFormatter* formatter = NULL;
58 error = CreateValueFormatter(config, formatter);
59 if (error != B_OK)
60 return error;
61
62 _formatter = formatter;
63
64 return B_OK;
65 }
66
67
68 status_t
GetTableCellValueEditor(Value * _value,Settings * settings,TableCellValueEditor * & _editor)69 EnumerationValueHandler::GetTableCellValueEditor(Value* _value,
70 Settings* settings, TableCellValueEditor*& _editor)
71 {
72 EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
73 if (value == NULL)
74 return B_BAD_VALUE;
75
76 IntegerValueFormatter::Config* config = NULL;
77 status_t error = CreateIntegerFormatterConfig(value, config);
78 if (error != B_OK)
79 return error;
80 BReference<IntegerValueFormatter::Config> configReference(config, true);
81
82 ValueFormatter* formatter;
83 error = CreateValueFormatter(config, formatter);
84 if (error != B_OK)
85 return error;
86 BReference<ValueFormatter> formatterReference(formatter, true);
87
88 TableCellEnumerationEditor* editor = new(std::nothrow)
89 TableCellEnumerationEditor(value, formatter);
90 if (editor == NULL)
91 return B_NO_MEMORY;
92
93 BReference<TableCellEnumerationEditor> editorReference(editor, true);
94 error = editor->Init();
95 if (error != B_OK)
96 return error;
97
98 editorReference.Detach();
99 _editor = editor;
100 return B_OK;
101 }
102
103
104 integer_format
DefaultIntegerFormat(IntegerValue * _value)105 EnumerationValueHandler::DefaultIntegerFormat(IntegerValue* _value)
106 {
107 EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
108 if (value != NULL && value->GetType()->ValueFor(value->GetValue()) != NULL)
109 return INTEGER_FORMAT_DEFAULT;
110
111 return IntegerValueHandler::DefaultIntegerFormat(_value);
112 }
113
114
115 status_t
CreateValueFormatter(IntegerValueFormatter::Config * config,ValueFormatter * & _formatter)116 EnumerationValueHandler::CreateValueFormatter(
117 IntegerValueFormatter::Config* config, ValueFormatter*& _formatter)
118 {
119 ValueFormatter* formatter = new(std::nothrow) EnumerationValueFormatter(
120 config);
121 if (formatter == NULL)
122 return B_NO_MEMORY;
123
124 _formatter = formatter;
125 return B_OK;
126 }
127
128
129 status_t
AddIntegerFormatSettingOptions(IntegerValue * _value,OptionsSettingImpl * setting)130 EnumerationValueHandler::AddIntegerFormatSettingOptions(IntegerValue* _value,
131 OptionsSettingImpl* setting)
132 {
133 EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
134 if (value != NULL
135 && value->GetType()->ValueFor(value->GetValue()) != NULL) {
136 status_t error = AddIntegerFormatOption(setting, "name", "Enum Name",
137 INTEGER_FORMAT_DEFAULT);
138 if (error != B_OK)
139 return error;
140 }
141
142 return IntegerValueHandler::AddIntegerFormatSettingOptions(_value, setting);
143 }
144
145
146 status_t
CreateTableCellValueRenderer(IntegerValue * _value,IntegerValueFormatter::Config * config,TableCellValueRenderer * & _renderer)147 EnumerationValueHandler::CreateTableCellValueRenderer(IntegerValue* _value,
148 IntegerValueFormatter::Config* config,
149 TableCellValueRenderer*& _renderer)
150 {
151 EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
152 if (value != NULL
153 && value->GetType()->ValueFor(value->GetValue()) != NULL) {
154 ValueFormatter* formatter = NULL;
155 status_t error = GetValueFormatter(value, formatter);
156 if (error != B_OK)
157 return error;
158 BReference<ValueFormatter> formatterReference(formatter,
159 true);
160
161 TableCellValueRenderer* renderer
162 = new(std::nothrow) TableCellFormattedValueRenderer(formatter);
163 if (renderer == NULL)
164 return B_NO_MEMORY;
165
166 _renderer = renderer;
167 return B_OK;
168 }
169
170 return IntegerValueHandler::CreateTableCellValueRenderer(_value, config,
171 _renderer);
172 }
173