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 "BoolValueHandler.h"
9
10 #include <new>
11
12 #include "BoolValue.h"
13 #include "BoolValueFormatter.h"
14 #include "TableCellBoolEditor.h"
15 #include "TableCellFormattedValueRenderer.h"
16
17
BoolValueHandler()18 BoolValueHandler::BoolValueHandler()
19 {
20 }
21
22
~BoolValueHandler()23 BoolValueHandler::~BoolValueHandler()
24 {
25 }
26
27
28 status_t
Init()29 BoolValueHandler::Init()
30 {
31 return B_OK;
32 }
33
34
35 float
SupportsValue(Value * value)36 BoolValueHandler::SupportsValue(Value* value)
37 {
38 return dynamic_cast<BoolValue*>(value) != NULL ? 0.5f : 0;
39 }
40
41
42 status_t
GetValueFormatter(Value * value,ValueFormatter * & _formatter)43 BoolValueHandler::GetValueFormatter(Value* value,
44 ValueFormatter*& _formatter)
45 {
46 if (dynamic_cast<BoolValue*>(value) == NULL)
47 return B_BAD_VALUE;
48
49 BoolValueFormatter* formatter = new(std::nothrow) BoolValueFormatter;
50 if (formatter == NULL)
51 return B_NO_MEMORY;
52
53 _formatter = formatter;
54 return B_OK;
55 }
56
57
58 status_t
GetTableCellValueRenderer(Value * value,TableCellValueRenderer * & _renderer)59 BoolValueHandler::GetTableCellValueRenderer(Value* value,
60 TableCellValueRenderer*& _renderer)
61 {
62 if (dynamic_cast<BoolValue*>(value) == NULL)
63 return B_BAD_VALUE;
64
65 ValueFormatter* formatter = NULL;
66 if (GetValueFormatter(value, formatter) != B_OK)
67 return B_NO_MEMORY;
68 BReference<ValueFormatter> formatterReference(formatter, true);
69
70 // create the renderer
71 TableCellValueRenderer* renderer
72 = new(std::nothrow) TableCellFormattedValueRenderer(formatter);
73 if (renderer == NULL)
74 return B_NO_MEMORY;
75
76 _renderer = renderer;
77 return B_OK;
78 }
79
80
81 status_t
GetTableCellValueEditor(Value * _value,Settings * settings,TableCellValueEditor * & _editor)82 BoolValueHandler::GetTableCellValueEditor(Value* _value, Settings* settings,
83 TableCellValueEditor*& _editor)
84 {
85 BoolValue* value = dynamic_cast<BoolValue*>(_value);
86 if (value == NULL)
87 return B_BAD_VALUE;
88
89 ValueFormatter* formatter;
90 status_t error = GetValueFormatter(value, formatter);
91 if (error != B_OK)
92 return error;
93 BReference<ValueFormatter> formatterReference(formatter, true);
94
95 TableCellBoolEditor* editor = new(std::nothrow)
96 TableCellBoolEditor(value, formatter);
97 if (editor == NULL)
98 return B_NO_MEMORY;
99
100 BReference<TableCellBoolEditor> editorReference(editor, true);
101 error = editor->Init();
102 if (error != B_OK)
103 return error;
104
105 editorReference.Detach();
106 _editor = editor;
107 return B_OK;
108 }
109